Loopj messing special characters before sending th

2020-04-01 01:36发布

问题:

I'm trying to send special characters through an http request, now I'm using Loopj as my http client. The problem is that when I try to send special characters i.e. "áéíóú" the request goes out with the characters "·ÈÌÛ˙", this is causing some issues on the server sider.

I've gone through the Loopj code and couldn't find anything relative to recoding my string or anything like it. In the worst case it seems like it would be encoded in UTF-8 which actually supports this characters.

Hope anyone can help.

Best Regards.

回答1:

I am guessing you mean AsyncHttpClient library, correct?

AHC defaults to encoding all I/O in UTF-8. Due to the lack of source code, I would point you to investigate the following:

  1. What is the encoding of the input? Make sure it's in UTF-8.
  2. Are you running the input through a filter/function that might change its encoding? Make sure that the filter/function produces UTF-8 also.
  3. Prior to checking what your backend actually receives, change your client to submit to http://httpbin.org/post and then check the result.
  4. If you receive correct submission in httpbin, and bad submission in your backend, the problem is NOT in AHC but in your backend.
  5. If you receive bad submissions in both httpbin and the backend, then the data being sent was originally bad or in a wrong encoding.

I hope this helps you find the problem quickly.



回答2:

Why Don't you use this Approach:

HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

HttpClient client = new DefaultHttpClient(httpParameters);
client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
client.getParams().setParameter("http.socket.timeout", new Integer(2000));
client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
HttpPost request = new HttpPost("http://www.server.com/some_script.php?sid=" + String.valueOf(Math.random()));
request.getParams().setParameter("http.socket.timeout", new Integer(5000));

List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// you get this later in php with $_POST['value_name']
postParameters.add(new BasicNameValuePair("value_name", "value_val"));

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);

in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String lineSeparator = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
    sb.append(line);
    sb.append(lineSeparator);
}
in.close();
String result = sb.toString();

Users of above code says, this code works like charm. And i think if you are facing issues with your approach then you should change your approach to solve your problem. See this Link which i found useful for you: Android default charset when sending http post/put - Problems with special characters