【问题标题】:Retrieving public tweets from twitter with Asynctask使用 Asynctask 从 twitter 中检索公共推文
【发布时间】:2017-04-15 16:16:41
【问题描述】:

您好,我正在尝试从 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;
    }
}

}

【问题讨论】:

标签: android


【解决方案1】:

如果您在 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 Twitter REST API documentation,如the documentation on their search API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    相关资源
    最近更新 更多