2013-04-17 20 views
0

我使用這段代碼從Reddit下載json數據。該代碼在我創建的測試Java項目中工作,但不會在我的Android設備上使用它時使用。在Android設備上,它只會下載一小部分json數據。我無法弄清楚爲什麼。感謝您的任何幫助。Android aynctask json

@Override 
protected JSONObject doInBackground(URL... urls) { 
    String c; 
    StringBuilder str = new StringBuilder(); 
     try { 
      BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) urls[0].getContent())); 
      while((c = in.readLine()) != null) { 
       str.append(c); 
      } 
      in.close(); 
      json = new JSONObject(str.toString()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    return json; 
} 
+0

你有沒有得到任何錯誤? – Vinay

+0

你能更具體嗎?你有什麼錯誤嗎?你怎麼知道它的一小部分數據? – jaga

+0

@JJPA沒有錯誤。 –

回答

0

試試這個辦法

public resultModel getJSONfromReddit() { 

     resultModel result = new resultModel(); 
     HttpResponse response; 
     HttpClient myClient = new DefaultHttpClient(); 
     HttpPost myConnection = new HttpPost("http://www.reddit.com/.json"); 

     try { 
      response = myClient.execute(myConnection); 
      String JSONString = EntityUtils.toString(response.getEntity(), 
        "UTF-8"); 
      JSONObject json = null; 
      json = new JSONObject(JSONString); 
      Log.i(TAG, JSONString); //this is the whole response but it'll be cutted if you only print it in the eclipse logcat 

      //PARSE YOUR JSON HERE 

     } 

     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return result; 
    } 

也許你的JSON在Eclipse控制檯板缺,你可以試圖讓解析JSON和打印最後的數據進行驗證,我希望我的回答是非常明顯的但如果你想問我關於如何解析JSON的答案(我假設你知道如何做到這一點),請隨時在評論中提問:)

+0

@NAYOSE謝謝,但我找到了問題。 –