2014-02-18 29 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(); 

}