2017-04-15 5 views
0

こんにちは私はtwitterからパブリックツイートを取得しようとしていますが、それについての説明はないようです。私は以下のコードを持っていますが、動作しません。これが旧式のコードであるかどうか、また、サンプルを見れば、それを見ていただければ幸いです。Asynctaskでtwitterからパブリックツイートを取得する

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    new MyTask().execute(); 

} 

private class MyTask extends AsyncTask<Void, Void, Void> { 
    private ProgressDialog progressDialog; 

    protected void onPreExecute() { 
     progressDialog = ProgressDialog.show(MainActivity.this, 
       "", "Loading. Please wait...", true); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     try { 

      HttpClient hc = new DefaultHttpClient(); 
      HttpGet get = new 
        HttpGet("http://search.twitter.com/search.json?q=android"); 

      HttpResponse rp = hc.execute(get); 

      if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
      { 
       String result = EntityUtils.toString(rp.getEntity()); 
       JSONObject root = new JSONObject(result); 
       JSONArray sessions = root.getJSONArray("results"); 
       for (int i = 0; i < sessions.length(); i++) { 
        JSONObject session = sessions.getJSONObject(i); 

        Tweet tweet = new Tweet(); 
        tweet.content = session.getString("text"); 
        tweet.author = session.getString("from_user"); 
        tweets.add(tweet); 
       } 
      } 
     } catch (Exception e) { 
      Log.e("TwitterFeedActivity", "Error loading JSON", e); 
     } 
     return null; 

    } 
    @Override 
    protected void onPostExecute(Void result) { 
     progressDialog.dismiss(); 
     setListAdapter(new TweetListAdaptor(
       MainActivity.this, R.layout.list_item, tweets)); 
    } 

} 

private class TweetListAdaptor extends ArrayAdapter<Tweet> { 

    private ArrayList<Tweet> tweets; 

    public TweetListAdaptor(Context context, 

          int textViewResourceId, 
          ArrayList<Tweet> items) { 
     super(context, textViewResourceId, items); 
     this.tweets = items; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) 
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.list_item, null); 
     } 
     Tweet o = tweets.get(position); 

     TextView tt = (TextView) v.findViewById(R.id.toptext); 
     TextView bt = (TextView) v.findViewById(R.id.bottomtext); 
     tt.setText(o.content); 
     bt.setText(o.author); 

     return v; 
    } 
} 

}

+0

常にhttpメソッドをAndroidとは独立してテストしてください –

+0

http://stackoverflow.com/q/35994186/7012517 – Shobhit

+0

Fabric twitter sdkを試してみませんかhttps://docs.fabric.io/android/twitter/overview .html –

答えて

0

Webブラウザでhttp://search.twitter.com/search.json?q=androidを訪問している場合は、表示されます。

{"errors":[{"message":"The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.","code":64}]} 

をすべてのアイデア、これは時代遅れコード

はいであれば、上記のエラーメッセージが示すとおりです。

the documentation on their search APIなどのthe Twitter REST API documentationを読むことができます。

関連する問題