开发者

Special characters pose problems with REST webservice communication

I am trying to post a JSON-object to a REST webservice from an Android application. Everything works fine until I add special characters like å, ä, ö.

JSONObject absenceObject = new JSONObject();
absenceObject.put(INFO_DESCRIPTION, "åka pendeltåg");
StringEntity entity = new StringEntity(absenceObject.toString());
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json";character);
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
Http开发者_Go百科Response response = httpclient.execute(httpPost);

If I print absenceObject.toString() and copy the result in to a regular rest client it works fine as well.


Try specifying the desired charset in the StringEntity constructor:

StringEntity entity = new StringEntity(absenceObject.toString(), "UTF-8");


If you control both ends of the pipe, you can encode the REST text as shown here Encoding/decoding REST path parameters


Re: Mark's response

Try specifying the desired charset in the StringEntity constructor:

StringEntity entity = new StringEntity(absenceObject.toString(), "UTF-8");

Note that setting charset after the constructor didn't work for me i.e.

entity.setContentEncoding("UTF-8");

I had to do as Mark said and set it in the constructor.

Michael


byte[] buf = body.getBytes(HTTP.UTF_8);
wr.write(buf, 0, buf.length);

Try this it will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜