JSON方法GET重音词áéé字符串返回NULL(Android)

时间:2014-02-18 22:51:48

标签: java android json

我的代码.java很好地将值发送到数据库,但是,当我使用方法GET列出我的数据库的值时,如果一个单词有重音符号,则该字符串返回null,为什么??

我已经使用UTF-8进行了测试,但它不起作用,我不知道该怎么做。拜托,我希望有人能帮助我!

// function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {
         DefaultHttpClient httpClient = new DefaultHttpClient();
         HttpEntity httpEntity = null;
         HttpResponse httpResponse = null;
        // Making HTTP request
        try {
              // http client


            // check for request method
            if (method == "POST") {

                // request method is POST
                // defaultHttpClient
                httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                httpResponse = httpClient.execute(httpPost);
                httpEntity = httpResponse.getEntity();

                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
           httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null){
                sb.append(line);
               response = sb.toString().substring(0, sb.toString().length()-1);
            }
            is.close();
            response = sb.toString().substring(0, sb.toString().length());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(response);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但我用过这个:

尝试更改

httpPost.setEntity(new UrlEncodedFormEntity(params));

httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

在方法帖子中

if (method == "POST") {

    // request method is POST
    // defaultHttpClient
    httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params, , HTTP.UTF_8));

    httpResponse = httpClient.execute(httpPost);
    httpEntity = httpResponse.getEntity();

    is = httpEntity.getContent();

}