2014-12-22 39 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