相关疑难解决方法(0)

使用HttpClient将带有post方法的utf8内容发送到android中的服务器

这是我的示例代码,但是我使用了HttpProtocolParams.setContentCharset(params,"utf-8"); 我的代码但是当我发送utf-8数据时,我收到的只是"?????" 在服务器端(PHP代码)!问题是什么?

    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "utf-8");

    HttpClient httpclient = new DefaultHttpClient(params);

    httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
    HttpPost httppost = new HttpPost("URL_TO_SERVER");

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("uname", name.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("uemail", email.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("udesc", body.getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse hresponse = httpclient.execute(httppost);

        String response = new Scanner(hresponse.getEntity().getContent(), "UTF-8").useDelimiter("\\A").next();

        hresponse.getEntity().consumeContent();
        httpclient.getConnectionManager().shutdown();

        Log.d("WX", response.toString());
        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
Run Code Online (Sandbox Code Playgroud)

java android http

8
推荐指数
1
解决办法
1万
查看次数

如何在UTF-8中通过http post发送字符串

我尝试发送字符串"Приветмир!"

String link = POST_URL;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(link);
String xml ="?????? ???";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("file", xml));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)

并通过PHP脚本保存:

if(!empty($_REQUEST['file'])){
$fp = fopen("C:\\windows\\temp\\1.xml", "w");
$mytext =$_POST["file"];
$test = fwrite($fp, $mytext); 
fclose($fp); 
Run Code Online (Sandbox Code Playgroud)

但是我得到了?????? ????? 在我的网络服务器上,我尝试使用utf编码重新打开文件,但它没有帮助.我该如何解决呢.

android

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

http ×1

java ×1