Android示例的简单JTwitter
我尝试了许多不同的方法,并在互联网上搜索,以找到将JTwitter与OAuth结合使用的教程。以下是我完成的步骤 下载Jtwitter和路标 将它们作为JAR添加到Java Builder中 创建了一个运行的简单按钮Android示例的简单JTwitter,android,oauth,jtwitter,Android,Oauth,Jtwitter,我尝试了许多不同的方法,并在互联网上搜索,以找到将JTwitter与OAuth结合使用的教程。以下是我完成的步骤 下载Jtwitter和路标 将它们作为JAR添加到Java Builder中 创建了一个运行的简单按钮 public class ShareGenerator extends Activity { private static final String JTWITTER_OAUTH_KEY = "this_is_populated"; private static
public class ShareGenerator extends Activity {
private static final String JTWITTER_OAUTH_KEY = "this_is_populated";
private static final String JTWITTER_OAUTH_SECRET = "this_is_populated";
Button menupopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
this.setContentView(R.layout.share);
this.txShare = (TextView)this.findViewById(R.id.lblshare);
this.menupopButton = (Button)this.findViewById(R.id.menupop);
menupopButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
TwitterSend();
}
});
}
我有我的课
public void TwitterSend () {
OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
Twitter jtwit = new Twitter("bob", client);
// open the authorisation page in the user's browser
client.authorizeDesktop();
// get the pin
String v = client.askUser("Please enter the verification PIN from Twitter");
client.setAuthorizationCode(v);
// Optional: store the authorisation token details
Object accessToken = client.getAccessToken();
// use the API!
jtwit.setStatus("Messing about in Java");
}
然而,我甚至不能让OAuth屏幕弹出。当它到达那里时就崩溃了。有人能帮我看看OAuth屏幕吗?导入设置正确。问题出在这一行,它使用java.awt.Desktop:
// open the authorisation page in the user's browser
client.authorizeDesktop();
这将在台式PC上运行,但在Android上不起作用
相反,从client.authorizeUrl()获取url
并将用户发送到那里。例如,像这样的东西:
URI url = client.authorizeUrl();
Intent myIntent = new Intent(Intent.VIEW_ACTION);
myIntent.setData(url);
startActivity(myIntent);
但我不是一个Android程序员!使用回调而不是
oob
,几乎肯定可以做得更好。希望其他人能够提供这方面的代码…请参阅本文[如果您愿意,请查看修订历史]Daniel提供了一个非常好的起点。这就是我在Android应用程序中实现的方式:
OAuthSignpostClient authClient = new OAuthSignpostClient('apiKey','apiSecret','callbackUrl');
java.net.URI jUrl = authClient.authorizeUrl();
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.encodedPath(jUrl.toString());
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(jUrl.toString()));
startActivity(myIntent);
当然,为了简洁起见,我使用了'apiKey'
等等。您需要在OAuthSignpostClient
构造函数中使用自己的密钥、密码和回调url
注意:您需要将JTwitter提供的Java.net.URI转换为Android.net.URI,以使用它来启动新的意图
在此之后,您需要使用意图过滤器捕获回调url,并对从Twitter API获得的用户令牌和密码进行处理。查看Logcat关于崩溃的信息,并确保您在清单中具有INTERNET权限。Logcat显示java.lang.NoClassDefFoundErrors:java.awt.Desktop它在客户端失败。authorizeDesktop();我无法将URI转换为URI以便在setData()方法中传递url。有什么建议吗?你会找到每一个相关的问题并提供相同的答案,不是吗?我有点困惑,为什么你在uriBuilder中有两行不使用的代码。encodedPath是否有隐藏的结果,或者我是否遗漏了什么?