2014-02-18 31 views
-1

我的代码.java将值发送到数据库非常好,但是,当我使用GET方法列出数据库的值时,如果一个字有重音符,字符串返回null,为什么?JSON方法GET重音字áéé字符串返回NULL(Android)

我已经用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; 

    } 
} 
+3

您无法将字符串与“==”进行比较,您需要使用“equals”方法 – user2340612

+0

数据是否以utf-8格式存储在数据库中? – Ring

+0

它可能是一个服务器端的问题?你能显示一个正在返回的GET请求的json对象吗? – AlexBrand

回答

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(); 

}