1

I am new to Java and trying to make native android application which includes making HTTP Calls to API Server. Now My issue is that for making HTTP POST (apache httpPost and httpClient) call with some JSON data. So to make StringEntity out of JSONObject I am writing this line of code:

StringEntity userDataStringEntity = new StringEntity(userDataString);

Where StringEntity is imported from import org.apache.http.entity.StringEntity;.

I have tried searching for this issue and I am finding same method with same "string" parameter.

Here are some links, but it didn't help me:

How to send a JSON object over HttpClient Request with Android?

How to send a JSON object over Request with Android?

Community
  • 1
  • 1
jimish
  • 654
  • 1
  • 8
  • 22

2 Answers2

2

That's definitely weird, by default the StringEntity goes for the charset "ISO-8859-1" which tells me that the userDataString is in another charset.

Either way, try:

StringEntity userDataStringEntity = new StringEntity(userDataString, "UTF-8");

This will work for utf-8 encoded strings.

Mário Fernandes
  • 1,822
  • 1
  • 21
  • 21
0

Perhaps unrelated, but I was getting an error at compile time, as the new StringEntity(str) wasn't wrapped in a try catch.

Might be of use to someone tho :)

David McEleney
  • 3,397
  • 1
  • 26
  • 32