android - 使用 json 数组的推特提要

标签 android json twitter

我正在开发一个应用程序来检索 Twitter 提要。我写了以下内容

try {   
    codeString x = "";
    Twitter y;
    HttpClient pingclient = new DefaultHttpClient();
    HttpPost pingpost = new HttpPost("https://twitter.com/statuses/user_timeline/krish_hari.json");
    pingpost.addHeader("Accepts", "application/json");
    pingpost.addHeader("Content-type", "application/json");

    org.apache.http.HttpResponse pingResponse = pingclient.execute(pingpost);
    HttpEntity loginEntity = pingResponse.getEntity();
    String result = EntityUtils.toString(loginEntity);

    //InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
    //byte [] buffer = new byte[is.available()];
    //while (is.read(buffer) != -1);
    //String jsontext = new String(buffer);
    JSONArray entries = new JSONArray(result);

    x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";

    int i;
    for (i=0;i<entries.length();i++) {
        JSONObject post = entries.getJSONObject(i);
        x += "------------\n";
        x += "Date:" + post.getString("created_at") + "\n";
        x += "Post:" + post.getString("text") + "\n\n";
    }
    tvData.setText(x);
}
catch (Exception je) {
    tvData.setText("Error w/file: " + je.getMessage());
}

我收到错误 error w/file:twitter.com。谁能帮我破解这个?

最佳答案

我使用此代码来检索 Twitter 提要:

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url[0]));
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuffer sb = new StringBuffer();
try
{
    String line = null;             
    while ((line = br.readLine())!=null)
    {
        sb.append(line);
        sb.append('\n');                
    }
}
catch (IOException e)
{
    e.printStackTrace();
}
String jsontext = new String(sb.toString());

url[0] 是 JSON 提要的 URL。

您必须重新格式化您的 URL,您现在使用的这种格式已被弃用。

引用https://dev.twitter.com/docs/api/1/get/statuses/user_timeline

关于android - 使用 json 数组的推特提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7979860/

相关文章:

android - GestureDetectorCompat 不工作?

android - 如何在Flutter中做隐式 Intent ?

android - fragment 中的操作栏项目单击处理程序

android - 在发布之前获取您的应用程序的链接

C# 大型 JSON 转字符串导致内存不足异常

ruby - 使用 Twitter Oauth 进行服务认证

python - Sqlalchemy - 如何修复 '_sa_instance_state'

javascript - 循环 JSON 对象并删除特定的子对象

java - JSON 简单的意外字符?

python - 如何使用 cron 在 Raspberry Pi 上自动化 Python 程序?