我正在开发一个检索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
。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
一些建议,它可能有效:
尝试这些并让我知道:
答案 1 :(得分:0)
试试这段代码:
检查Log cat中的响应
public class List extends Activity {
String response_data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Load_Task().execute();
}
public class Load_Task extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(List.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try{
HttpClient client = new DefaultHttpClient();
String getURL = "http://twitter.com/statuses/user_timeline/krish_hari.json";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
response_data=EntityUtils.toString(resEntityGet);
}
Log.v("aaaa", ""+response_data);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
答案 2 :(得分:0)
我使用此代码检索Twitter Feed:
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 Feed的网址。
您必须重新格式化您的网址,不推荐使用您现在使用的格式。
请参阅https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
答案 3 :(得分:0)
您似乎需要添加android.permission.INTERNET
<uses-permission android:name="android.permission.INTERNET" />