gpt4 book ai didi

android - 带有 UTF8 文本的 POST 以 ASCII 形式出现

转载 作者:行者123 更新时间:2023-11-30 03:40:18 25 4
gpt4 key购买 nike

我用这段代码奋斗了 4 个小时,也许我做错了什么。“文本”像“????”一样进入服务器

我检查了日志:

error_log(mb_detect_encoding($_POST['text']))

它给我 ASCII。

   /**
* Send the question to the server
*/
private void sendQuestion() {
final String uid = getContext().getSharedPreferences(LoginActivity.PREFS_NAME, 0).getString("uid", "0");
final String text = ((EditText) findViewById(R.id.question_et)).getText().toString();

final MultipartEntity multipartEntity = new MultipartEntity();

if (mFile != null) {
final FileBody fileBody = new FileBody(mFile);
multipartEntity.addPart("userfile", fileBody);
}

try {
multipartEntity.addPart("uid", new StringBody(uid));
multipartEntity.addPart("type", new StringBody(mFile == null ? "1" : "2"));
multipartEntity.addPart("category", new StringBody(String.valueOf(mCategoryId + 1)));
multipartEntity.addPart("text", new StringBody(URLEncoder.encode(text, "UTF-8")));

} catch (UnsupportedEncodingException e) {
}

final HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

final HttpPost httpPost = new HttpPost("http://site.ru/add.php");
httpPost.setEntity(multipartEntity);

final HttpClient httpClient = new DefaultHttpClient(httpParameters);

new AsyncTask<Void, Void, Void>() {

@Override
protected void onPreExecute() {
super.onPreExecute();

Toast.makeText(getContext(), "Ваш вопрос отправлен, ждите ответа", Toast.LENGTH_LONG).show();
}

@Override
protected Void doInBackground(Void... voids) {
try {
final HttpResponse httpResponse = httpClient.execute(httpPost);
final int code = httpResponse.getStatusLine().getStatusCode();
final String response = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

}.execute();
}

最佳答案

这里是正确答案(根据评论:))

尝试直接在 StringBody 对象上设置编码。

通过使用 StringBody(String text, Charset charset) 代替

如果不设置编码,将使用 US-ASCII 字符集,这不是您想要的。

JavaDoc for Stringbody constructor

关于android - 带有 UTF8 文本的 POST 以 ASCII 形式出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853697/

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