gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:31 26 4
gpt4 key购买 nike

这是我的示例代码,但是我使用了 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
}

最佳答案

也许您应该为实体使用 UTF-8:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

关于java - 使用HttpClient将带有post方法的utf8内容发送到android中的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8964291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com