从Android发送Web服务请求时,它在Web端成为问号(?)。
{CategoryId=5; CategoryName=開胃; CategoryDesc = 開胃; }
但在应用中,它显示为&#38283,&#32963
答案 0 :(得分:3)
将您的字符串发送到服务器
String encodedString = URLEncoder.encode(yourString, "UTF-8");
从服务器获取字符串
String decodedString = URLDecoder.decode(yourString, "UTF-8");
答案 1 :(得分:0)
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;
}