gpt4 book ai didi

android - 如何使用 HttpPost 发送日语字符

转载 作者:太空狗 更新时间:2023-10-29 16:04:04 24 4
gpt4 key购买 nike

我正在尝试将日语字符发送到我的 API 服务器,但发送的字符出现乱码并变成了 ????。所以我使用以下方法将编码设置为实体:

    StringEntity stringEntity = new StringEntity(message, "UTF-8");

但输出变成了 org.apache.http.entity.StringEntity@4316f850。我想知道是否将 stringEntity 转换为字符串会导致此问题,因为我想将它作为 String 在我的服务器中发送。

我是这样用的:

public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();

StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();

String result = convertStreamToString(is);
return result;
}

我哪里可能出错了?

最佳答案

您不需要使用 StringEntity

List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));

相反,您必须传递第二个参数来初始化 `UrlEncodedFormEntity。

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");

关于android - 如何使用 HttpPost 发送日语字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447019/

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