简体   繁体   中英

how to send unicode charcters in http post request from asyncTask

I saw this post How to send unicode characters in an HttpPost on Android but I usaully do request in this way in AsyncTask class.My log is also printing local language in urlParameters but server is returning no result while it is perfect for english Strings:

@Override
protected String doInBackground(String... URLs) {

    StringBuffer response = new StringBuffer();

    try {
        URL obj = new URL(URLs[0]);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // add request header
        con.setRequestMethod("POST");

        if (URLs[0].equals(URLHelper.get_preleases)) {
            urlCall = 1;
        } else
            urlCall = 2;

        // String urlParameters = "longitude=" + longitude + "&latitude="+latitude;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());

        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        if (responseCode == 200) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response.toString();
}

Is there a way to set character set UTF-8 to request parameters coding this way?

String urlParameters = "longitude=" + longitude + "&latitude="+latitude;

You need to URL-encode components you are injecting into an application/x-www-form-urlencoded context. (Even aside from non-ASCII characters, characters like the ampersand will break otherwise.)

Specify the string-to-bytes encoding that you are using for your request in that call, for example:

String urlParameters = "longitude=" + URLEncoder.encode(longitude, "UTF-8")...

...

DataOutputStream wr = new DataOutputStream(con.getOutputStream());

A DataOutputStream is for sending struct-like Java-typed binary data down a stream. It doesn't give you anything you need for writing HTTP request bodies. Maybe you meant OutputStreamWriter ?

But since you already have the string all in memory you could simply do:

con.getOutputStream().write(urlParameters.getBytes("UTF-8"))

(Note the UTF-8 here is somewhat superfluous. Because you will already have URL-encoded all the non-ASCII characters into %xx escapes, there will be nothing to UTF-8-encoded. However in general it is almost always better to specify a particular encoding than omit it and revert to the unreliable system default encoding.)

new InputStreamReader(con.getInputStream())

is also omitting the encoding and reverting to the default encoding which is probably not the encoding of the response. So you will probably find non-ASCII characters get read incorrectly in the response too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM