我正在开发一个应用程序来检索 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/