问题描述
当从android发送Web服务请求时,它在Web端成为问号(?)。
{CategoryId=5; CategoryName=開胃; CategoryDesc = 開胃; }
但在应用程序中,它显示为&#38283,&#32963
1楼
将您的字符串发送到服务器时
String encodedString = URLEncoder.encode(yourString, "UTF-8");
从服务器获取字符串时
String decodedString = URLDecoder.decode(yourString, "UTF-8");
2楼
我回答得有点迟,但希望它能帮助别人。
我遇到了同样的问题,并通过在我的情况下将CharacterSet
设置为utf-8
和Content-Type
到application/json
来解决。
您可以根据自己的要求选择其他内容类型。
见下文
StringEntity s = new StringEntity(data.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
以下是完整的方法。
public static String doPost(JSONObject data, String url)
throws ClientProtocolException, IOException, TimeoutException, ApiException {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000; // 30 Seconds
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 50000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Authorization", "Basic " + addAuthHeader());
Log.d("TAG", "Data ==> " + data.toString());
StringEntity s = new StringEntity(data.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(s);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String status = getASCIIContentFromEntity(entity);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
status = "ApiError";
throw new ApiException();
}
Log.d("TAG", "Response String ====== " + status);
return status;
}