2013-05-07 81 views
-1

我有JSON解析器的以下代碼。其實我沒有自己寫過,我從各種stackoverflow問題收集它。但不知何故,它不起作用。我給使用互聯網的權限:異步JSON解析器不起作用

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    String url=null; 
    // constructor 
    public JSONParser() { 

    } 

    // function get JSON from URL 
    public JSONObject makeHttpRequest(String url) { 
     BackGroundTask Task= new BackGroundTask(url); 
     try { 
      return Task.execute().get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 
    } 
    public class BackGroundTask extends AsyncTask<String, String, JSONObject>{ 
     String URL=null; 
     public BackGroundTask(String url) { 
      URL = url; 
     } 
     @Override 
     protected JSONObject doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      // Making HTTP request 
      try { 
       // Making HTTP request 

       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity 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, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
      } catch (Exception e) { 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 

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

      // return JSON String 
      return jObj; 

     } 
    } 
} 

編輯:

你可以看到,可能會導致此類失敗的任何問題的一部分?我調試我的代碼,並發現該行

JSONParser jParser = new JSONParser(); 
JSONObject json = jParser.makeHttpRequest(URL); 

json對象是null後。

+1

又是什麼「但不知何故,它不工作」是什麼意思?錯誤? – codeMagic 2013-05-07 13:45:41

+0

我不小心按下了提問問題鏈接,您可以在編輯中找到更多信息。 – 2013-05-07 13:46:57

+2

你不應該從'AsyncTask'使用'get()'...這幾乎是一樣的,就像你根本不會使用'AsyncTask'一樣... – Selvin 2013-05-07 13:48:49

回答

0

AsyncTasks並不適合在Android上聯網。正如文件所述:它們是爲了「短暫的生活任務」而設計的。網絡不屬於這一類。而且,使用異步任務會導致活動生命週期中的一些重要問題。爲了讓人信服,只需旋轉設備,就會發現您的網絡請求無法更新您的用戶界面。

我建議你看看RoboSpice,這個圖書館在網絡方面更有意思。而且,spring的android模塊會讓你很容易地解析json。

順便說一句,我是RS的作者之一......

+0

我發佈了另一個關於如何在Android上爲JSON編寫任何解析器的問題,幾乎每個人都提供了我異步執行的解決方案。你可以從[這裏](http://stackoverflow.com/questions/16418425/parsing-a-json-file-in-android-application)得到這個問題。如果我開始學習其他東西,這將是非常糟糕的。 – 2013-05-07 13:54:41

+0

RS旨在使它*非常容易異步執行網絡請求。至少要簡單看看它。很確定你會喜歡它。 – Snicolas 2013-05-07 14:40:10