2013-03-28 54 views
0

使用此功能,我的JSON數據到的對象讀取,然後嘗試打印其ID:Android的JSON請求 - JSON匹配異常

private void getJsonData(String url) { 
    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(url); 
    try { 
     HttpResponse response = client.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { // success! 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
      JSONObject jsonObject = new JSONObject(builder.toString()); 
      for (int i = 0; i < jsonObject.length(); i++) { 
       Toast.makeText(context, jsonObject.getString("id"), Toast.LENGTH_LONG).show(); 
      } 
     } else { 
      Log.e(">>", "Failed to download file"); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

我得到這個錯誤:http://i.imgur.com/CkFOJmk.jpg

有任何想法嗎?在該行它的錯誤:

HttpEntity entity = response.getEntity(); 

我的PHP:

$this->db->select('id, unit_number'); 
    $this->db->from('parts'); 
    $this->db->order_by('unit_number', 'desc'); 
    $query = $this->db->get(); 
    echo json_encode($query->result()); 
+0

意味着ü沒有得到有效的JSON字符串形式的服務器PLZ其中U是從服務器獲取 –

+0

@ρяσѕρєяK看起來好像我的JSON在最後因爲某些原因中旬對象獲得截止後充分反應。嗯.. – user2011736

回答

0

不知道,但目前看來,JSON,你從你的服務器發送是由Android使用的標準JSON格式有些不同(?)。無論如何,會更好地看到你從服務器獲得的完整字符串響應。

確保您收到的是JSON而不是HTML。 Error Android - received HTML instead of JSON