Send character with UTF-8 from Android app to server side

Viewed 804

I have some services with the following header and I want to call these methods in Android application. I wrote the following code for calling service is correct but if I add charset=utf-8 to the header, I get 400 error code. I should send Persian character in some methods and without UTF-8, I get incorrect characters on the server-side. Anyway, Please send your suggestion to edit my code.

Another note: I work with PostMan and post Persian character to the service and it shows correct characters.

WCF Header method:

     OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "Test")]

Android Code:

         this.jsonStringer=params[0].toString();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            StringEntity msg = new StringEntity(jsonStringer);
            msg.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            msg.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            request.setEntity(msg);
            response = httpClient.execute(request);

When I add request.setHeader("Content-type", "application/json; charset=utf-8"); I get 400 error code!

2 Answers

Header parameters are by default UTF8 but if you are trying to pass your parameters by putting them in the Body, you should do as follow.

Assume you have stored your non utf8 string in value1. First convert your string to utf8 using URLEncoder then add it to your jsonObject and cast it as body.

Here it is a sample code but in Kotlin!

val jsonObject = JSONObject()
jsonObject.put(“param1”,val1)
val val1 = URLEncoder.encode(value1,”utf8”)
jsonObject.put(“param2”,val2) // and so on
val body = jsonObject.toString().toRequestBody(“application/json; charset=utf-8”.toMediaTypeOrNull())

Important Notice

Make sure you string is not urf8 because unless you are reading it from an ANSI txt file, they are Unicode and you do not need to convert them. If you don’t see the result as you should see on the server, that is another issue.

This may be caused by the difference between the header you set in the request and the header you set in the basicheader.You can set their content-type to the same format.This setting in Java can solve this problem,you can try it in your program.

     public static void main(String args[]) throws ClientProtocolException, IOException { 

     String json="{\"user\":{\"Email\":\"123\",\"Name\":\"sdd\",\"Password\":\"sad\"}}";
     CloseableHttpClient httpClient=HttpClientBuilder.create().build();
     HttpPost request = new HttpPost("http://localhost:8012/ServiceModelSamples/service/web/user");
     request.setHeader("Accept", "application/json");

     request.setHeader("Content-Type", "application/json;charset=utf-8");

     StringEntity msg = new StringEntity(json);
     msg.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
           "application/json;charset=utf-8"));
     msg.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
           "application/json"));
     request.setEntity(msg);
     HttpResponse response = httpClient.execute(request); 
     System.out.println(response); 
    } 
Related