2013-04-04 48 views
-2

我正在关注thenewboston Android tutorials on Youtube,并且在解析JSON数据方面遇到了一些问题。NullPointerException在Android中执行HTTP GET请求..任何想法?

从我所知道的,问题似乎是在TwitterTask子类行:

r = client.execute(get); 

我相当肯定我用的URL是正确的,因为链接我的一部分发送GET请求工作在我的浏览器:

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ssjunejo

我会非常感谢任何帮助。提前谢谢了。这里是我的代码:

public class JSONMainActivity extends Activity { 

    TextView httpStuff; 
    HttpClient client; 
    JSONObject json; 

    String URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name="; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_jsonmain); 
     httpStuff = (TextView) findViewById(R.id.tvJson); 
     new TwitterReadTask().execute("text"); 

    } 

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

    public class TwitterReadTask extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      try { 
       json = lastTweet("ssjunejo"); 
       return json.getString(params[0]); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     public JSONObject lastTweet(String username) 
       throws ClientProtocolException, IOException, JSONException { 
      StringBuilder url = new StringBuilder(URL); 
      url.append(username); 
      System.out.println("YOLO " + url.toString()); 
      HttpGet get = new HttpGet(url.toString()); 
      System.out.println("GET CREATED"); 
      HttpResponse r = null; 
      try { 
       r = client.execute(get); 
      } catch (Exception e) { 
       System.out.println("OOPS. ERROR"); 
       e.printStackTrace(); 
      } 
      System.out.println("Executed get request"); 
      int status = r.getStatusLine().getStatusCode(); 
      if (status == 200) { 
       HttpEntity e = r.getEntity(); 
       String data = EntityUtils.toString(e); 
       JSONArray timeline = new JSONArray(data); 
       JSONObject last = timeline.getJSONObject(0); 
       return last; 
      } else { 
       Toast.makeText(JSONMainActivity.this, "error", 
         Toast.LENGTH_SHORT); 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      System.out.println(result); 
      httpStuff.setText(result); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      System.out.println("TESTING"); 
     } 
    } 
} 
+0

怎么样堆栈跟踪:setContentView后或调用client.execute方法之前初始化它作为活动的onCreate方法?错误日志? – 2013-04-04 11:09:19

+1

请显示堆栈跟踪。否则很难说 – PSR 2013-04-04 11:09:33

+1

npe =>堆栈跟踪。没有stacktrace => -1 – njzk2 2013-04-04 11:25:46

回答

2

你忘了初始化HttpClientclient对象。

HttpClient client= new DefaultHttpClient(); 
+0

哦,我的上帝。奥卡姆剃须刀的头部很丑陋。它现在有效!非常感谢你指出这一点! – SJunejo 2013-04-04 11:21:18