Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从asyncTask发送http post请求中的unicode字符_Android_Unicode_Android Asynctask - Fatal编程技术网

Android 如何从asyncTask发送http post请求中的unicode字符

Android 如何从asyncTask发送http post请求中的unicode字符,android,unicode,android-asynctask,Android,Unicode,Android Asynctask,我看到了这篇文章,但我通常在AsyncTask类中以这种方式进行请求。我的日志也在urlParameters中打印本地语言,但服务器没有返回任何结果,因为它非常适合英文字符串: @Override protected String doInBackground(String... URLs) { StringBuffer response = new StringBuffer(); try { URL obj = new URL(URLs[0]);

我看到了这篇文章,但我通常在AsyncTask类中以这种方式进行请求。我的日志也在urlParameters中打印本地语言,但服务器没有返回任何结果,因为它非常适合英文字符串:

@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();
}
有没有办法将字符集UTF-8设置为以这种方式编码请求参数

字符串urlParameters=“经度=“+经度+”&纬度=“+纬度

您需要对注入到
应用程序/x-www-form-urlencoded
上下文中的组件进行URL编码。(除非ASCII字符外,其他字符(如与符号)也会中断。)

指定在该调用中用于请求的字符串到字节编码,例如:

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

DataOutputStream wr=新的DataOutputStream(con.getOutputStream())

DataOutputStream
用于沿流发送类似于Java类型的结构化二进制数据。它没有提供编写HTTP请求体所需的任何信息。也许你的意思是
OutputStreamWriter

但由于您已经将字符串全部存储在内存中,您只需执行以下操作:

con.getOutputStream().write(urlParameters.getBytes("UTF-8"))
(注意这里的
UTF-8
有些多余。因为您已经将所有非ASCII字符编码为
%xx
转义符,所以UTF-8编码将不会有任何内容。但是,一般来说,指定特定编码总比忽略它并恢复为不可靠的系统默认编码要好。)

新的InputStreamReader(con.getInputStream())


也省略了编码并恢复为默认编码,该编码可能不是响应的编码。因此,您可能会在响应中发现非ASCII字符的读取错误。

@SilentKiller这就是他所发布的问题:)@DIVA请检查上述代码中是否有这一行
StringEntity StringEntity=new StringEntity(msg,“UTF-8”)他没有在任何地方提供字符集。@SilentKiller你说得对,你提供的链接与他提供的相同。。。