2014-12-22 27 views
0

我是新來的Android和JSON和我試圖檢索雅虎氣象服務的一些JSON數據,並.setText在我的三個textviews在我的XML相關數據的JSON對象Android JSONObject結果始終爲空?

這是在那裏我得到了休息查詢(點擊測試,你會得到休息查詢):

https://developer.yahoo.com/yql/console/#h=select+*+from+weather.forecast+where+woeid%3D2502265 

錯誤:

12-22 19:39:10.745 31404-31431/eggy.com.jsontest E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 
    Process: eggy.com.jsontest, PID: 31404 
    java.lang.RuntimeException: An error occured while executing doInBackground() 
      at android.os.AsyncTask$3.done(AsyncTask.java:300) 
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      at java.lang.Thread.run(Thread.java:841) 
    Caused by: java.lang.NullPointerException 
      at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 
      at org.json.JSONTokener.nextValue(JSONTokener.java:94) 
      at org.json.JSONObject.<init>(JSONObject.java:155) 
      at org.json.JSONObject.<init>(JSONObject.java:172) 
      at eggy.com.jsontest.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:81) 
      at eggy.com.jsontest.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:41) 
      at android.os.AsyncTask$2.call(AsyncTask.java:288) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
            at java.lang.Thread.run(Thread.java:841) 

代碼:

public class MainActivity extends ActionBarActivity { 
    private static String yahooWeatherInfo = 
      "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D2502265&format=json&diagnostics=true&callback="; 
    private static String chill = ""; 
    private static String direction = ""; 
    private static String speed = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new MyAsyncTask().execute(); 
    } 

     private class MyAsyncTask extends AsyncTask<String,String,String> { 

      @Override 
      protected String doInBackground(String... params) { 

       DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
       HttpPost httppost = new HttpPost(yahooWeatherInfo); 

       // Depends on your web service 
       httppost.setHeader("Content-type", "application/json"); 

       InputStream inputStream = null; 
       String result = null; 
       try { 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 

        inputStream = entity.getContent(); 
        // json is UTF-8 by default 
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
        StringBuilder sb = new StringBuilder(); 

        String line = null; 
        while ((line = reader.readLine()) != null) 
        { 
         sb.append(line + "\n"); 
        } 
        result = sb.toString(); 
       } catch (Exception e) { 
        // Oops 
       } 
       finally { 
        try{ 
         if(inputStream != null) 
          inputStream.close(); 
        }catch(Exception squish){ 

        } 
       } 
       try { 
        JSONObject jObject = new JSONObject(result); 
        JSONObject queryObject = jObject.getJSONObject("query"); 
        JSONObject windObject = queryObject.getJSONObject("wind"); 
        chill = windObject.getString("chill"); 
        direction = windObject.getString("direction"); 
        speed = windObject.getString("speed"); 
       } catch(JSONException e) { 

       } 
       return null; 
      } 
        @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      TextView line1 = (TextView) findViewById(R.id.line1); 
      TextView line2 = (TextView) findViewById(R.id.line2); 
      TextView line3 = (TextView) findViewById(R.id.line3); 
      line1.setText("Chill " + chill); 
      line2.setText("Direction " + direction); 
      line3.setText("Speed " + speed); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

錯誤總是在:

JSONObject jObject = new JSONObject(result); 

也許我用錯了查詢,我不能完全肯定。

謝謝你的幫助。

+1

你檢查什麼結果,當執行涉及到該行? –

+1

那麼,如果您的HTTP請求有任何問題,結果將爲空。嘗試從null分析JSONObject將是一個問題。您是否嘗試檢查運行時的結果? –

+0

嗨,大家好,我嘗試記錄結果,但我甚至得到一個空指針異常 - 由於:java.lang.NullPointerException:println需要一條消息。我認爲沒有任何問題。 – Adz

回答

0

試試這個

JSONObject jObject = new JSONObject(result); 
JSONObject windObject = jObject.getJSONObject("query").getJSONObject("results").getJSONObject("channel").getJSONObject("wind"); 

遵循嵌套對象以JSON