如何在Web服务请求中发送中文时管理中文。

时间:2014-03-10 18:46:06

标签: android web-services multilingual

从Android发送Web服务请求时,它在Web端成为问号(?)。

{CategoryId=5; CategoryName=開胃; CategoryDesc = 開胃; }

但在应用中,它显示为&#38283,&#32963

2 个答案:

答案 0 :(得分:3)

将您的字符串发送到服务器

String encodedString = URLEncoder.encode(yourString, "UTF-8");

从服务器获取字符串

String decodedString = URLDecoder.decode(yourString, "UTF-8");

答案 1 :(得分:0)

我很晚才回答,但希望能帮助别人。我遇到了同样的问题,并通过在我的案例中将CharacterSet设置为utf-8Content-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;
}