Android HttpRequestHeader Json解析错误

时间:2015-12-24 09:40:38

标签: android json

实际上我在JSONParser中使用了这个标题 conn.setRequestProperty(“Accept-Charset”,“UTF-8”);

如何更正?

3 个答案:

答案 0 :(得分:1)

你可以试试这个

HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(json, HTTP.UTF_8);

答案 1 :(得分:0)

你可以试试这个

   HttpPost httpPost = new HttpPost(url);

    StringEntity se = new StringEntity(json, HTTP.UTF_8);

    // 6. set httpPost Entity
    httpPost.setEntity(se);

    // 7. Set some headers to inform server about the type of the content        
    httpPost.setHeader("Content-type", "application/json; charset=utf-8");
希望它有效。

答案 2 :(得分:0)

参数是您的HashMap参数

urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("POST");

        if (parameters != null) {
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(2000);
            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    os, "UTF-8"));
            writer.write(getQuery(parameters, sendingParametersType));
            writer.flush();
            writer.close();
            os.close();
        }

        urlConnection.connect();

并使用此功能分离出参数& JSON

 private String getQuery(HashMap<String, String> params, String typeJsonOrParams)
        throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();

    if (AppContants.JSON.equalsIgnoreCase(typeJsonOrParams)) {//JSON
        result = new StringBuilder(new JSONObject(params).toString());
    } else if (AppContants.PARAMETERS.equalsIgnoreCase(typeJsonOrParams)) {//IF Parameters

        boolean first = true;

        for (Entry<String, String> e : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(e.getKey(), "UTF-8"));
            result.append("=");
            if (e.getValue().equalsIgnoreCase("")) {
                result.append("");
            } else {
                result.append(URLEncoder.encode(e.getValue(), "UTF-8"));
            }
        }
    } else {

    }

    return result.toString();
}