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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Mario, Thanks for the answer. I figured this out too.. and one more thing, There was error of Unhandled exception also.
@Jimish good to know :) not the Unhandled exception of course but that you figured it out ;)
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.