0

I'm trying to make a POST request using JSON with foreign characters, such as the Spanish n with the '~' over it, but I keep getting this request and response error:

POST ...
Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 151
Content-Encoding: UTF-8
Host: ...
Connection: Keep-Alive
User-Agent: ..

{"numbers":"2","date":"2014-07-15T00:00:00+0000","description":" // this never gets closed

X-Powered-By: ...
Set-Cookie: ...
Cache-Control: ...
Date: Tue, 15 Jul 2014 15:19:12 GMT
Content-Type: application/json
Allow: GET, POST

{"status":"error",
"status_code":400,
"status_text":"Bad Request",
"current_content":"",
"message":"Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)"}

I can already make a successful POST request with normal ASCII characters, but now that I'm supporting foreign languages, I need to convert the foreign characters to UTF-8 (or whatever the correct encoding ends up being), unless there's a better way to do this.

Here's my code:

JSONObject jsonObject = new JSONObject();
HttpResponse resp = null;
String urlrest = // some url;
HttpPost p = new HttpPost(urlrest);
HttpClient hc = new DefaultHttpClient();
hc = sslClient(hc);

try
{
   p.setHeader("Accept", "application/json");
   p.setHeader("Content-Type", "application/json");

   // setting TimeZone stuff

   jsonObject.put("date", date);
   jsonObject.put("description", description);
   jsonObject.put("numbers", numbers);

   String seStr = jsonObject.toString();
   StringEntity se = new StringEntity(seStr);
   // Answer: The above line becomes new StringEntity(seStr, "UTF-8");

   Header encoding = se.getContentType();
   se.setContentEncoding("UTF-8");
   se.setContentType("application/json");
   p.setEntity(se);
   resp = hc.execute(p);

When I put a breakpoint and look at se before it's submitted, the characters look right.

UPDATE: code updated with answer a few lines above with a comment identifying it.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
craned
  • 2,991
  • 2
  • 34
  • 38
  • Is this possible: `jsonObject.toString("UTF-8");`? – greenapps Jul 15 '14 at 16:38
  • No, the only parameter `toString` accepts is an `int` for a number of indents. – craned Jul 15 '14 at 16:41
  • Did you try to put in description without all these conversions? – greenapps Jul 15 '14 at 16:46
  • Yes, that's what produced this error in the first place. – craned Jul 15 '14 at 16:49
  • 1
    Try to give `new StringEntity...` an extra parameter for utf-8. – greenapps Jul 15 '14 at 16:51
  • Near as I can tell, an extra parameter not an option for StringEntity. I appreciate your suggestions, but would you mind looking them up before posting them to make sure the Java API actually allows them? – craned Jul 15 '14 at 17:06
  • No you look them up. Don't be so lazy. You have to do the work. And you are wrong: that works. – greenapps Jul 15 '14 at 17:13
  • I personally try to only post answers I've tried myself that I know work so I don't waste others' time, and many others do the same so I could say the same to you. Please show me the code because I'm not finding it! Maybe I'm interpreting your answer wrong. – craned Jul 15 '14 at 17:26
  • Be glad that i help you with hints. Don't command me please. Don't teach me. And do the work: A small search/google for 'stringentity utf-8' will show you how to use that parameter. – greenapps Jul 15 '14 at 17:26
  • You're right; it worked. Thank you. My previous 5+ hours of work was not yielding much. I apologize for commanding you. Before you commanded me, I had already changed my post. In the future, you might consider being a little more explicit if you choose not to use code. I misread your post saying to add an extra parameter for utf-8 in an HTTP context, thinking there was an addParameter instance method for StringEntity. Would you like to create a post that shows the code so I can give you credit for the answer? I'd rather give you the credit directly than have to reference you in a post I make. – craned Jul 15 '14 at 18:01
  • You are busy programming on your pc with all resources at hand. I'm doing other things far from pc and resources and my code. Meanwhile i try to help you with hints writing on a small phone. Don't expect an essay. Be glad with what you get. You have to do the work. – greenapps Jul 15 '14 at 18:11
  • Just a suggestion. I understand having only a small phone, not asking for an essay. Thanks again! – craned Jul 15 '14 at 18:14

1 Answers1

2

The new StringEntity constructor takes a "UTF-8" parameter.

craned
  • 2,991
  • 2
  • 34
  • 38
greenapps
  • 11,154
  • 2
  • 16
  • 19