android 上网,导致 twitter4j 异常?
我正在 android 上尝试 twitter4j(这两个都是新的),用 java 编写了一个简单的过程只是为了测试它。它下载用户时间线并打印到屏幕上。
我修改了 Android 的代码,但当我尝试下载用户时间线时出现 TwitterException。我检查了调试器,异常为空;没有给出任何信息。我还根据之前的建议将互联网权限添加到了 android 清单中。代码如下:
package com.test;
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.os.Bundle;
public class Test2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Status> statuses = null;
Twitter api = new TwitterFactory().getInstance("USERNAME","PASSWORD");
try{
statuses = api.getUserTimeline();
}
catch(TwitterException e){
System.out.println("ERROR");
System.exit(-1);
}
for(Status s: statuses){
System.out.println(s.getText());
}
}
}
我意识到这只打印到控制台,只是为了保持简单。
感谢您的任何和所有帮助。
I'm experimenting with twitter4j on android (new to both) coded up a simple process in java just to test it out. It downloads a users timeline and prints to screen.
I modify the code for android, but I get a TwitterException when i try to download the user timeline. I checked out the debugger and the exception is null; no information given. I've also added the Internet permission to the android manifest on previous advice. Heres the code:
package com.test;
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.os.Bundle;
public class Test2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Status> statuses = null;
Twitter api = new TwitterFactory().getInstance("USERNAME","PASSWORD");
try{
statuses = api.getUserTimeline();
}
catch(TwitterException e){
System.out.println("ERROR");
System.exit(-1);
}
for(Status s: statuses){
System.out.println(s.getText());
}
}
}
I realise this only prints to the console, just to keep it simple.
Thanks for any and all help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您在
AndroidManifest.xml
文件中拥有INTERNET
权限。另外,不建议在 Android 上使用
System.out.println()
。请使用android.util.Log
类并将调试输出发送到 LogCat(可通过adb logcat
、DDMS 或 Eclipse 中的 DDMS 透视图获取)。Make sure you have the
INTERNET
permission in yourAndroidManifest.xml
file.Also
System.out.println()
is not recommended on Android. Please use theandroid.util.Log
class and send your debugging output to LogCat (available viaadb logcat
, DDMS, or the DDMS perspective in Eclipse).请检查您的时间戳。每个HttpRequest都包含当前时间戳,如果时间戳错误则抛出异常。
Please check your timestamp. Each HttpRequest contain current timestamp, if the timestamp is wrong then it throw exception.