我试着写一个应用程序,一个用户可以在Twitter上登录。我用twitter4j像library.My问题是,当我走在页,在这里我必须把用户名和密码,程序块,因为我不知道使用回调排在我的应用程序。有人可以帮我?
公共类MainActivity延伸活动{
私人微博微博;
RequestToken requestToken;
最终公共静态字符串CALLBACK_SCHEME =X-latify,支持OAuth的Twitter;
最终公共静态字符串CALLBACK_URL = CALLBACK_SCHEME +://回调;
私人开放的URI;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮B =(按钮)findViewById(R.id.button1);
b.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
新updateTwitterStatus()执行()。
}
});
}
@覆盖
保护无效的onDestroy(){
twitter.shutdown();
}
类updateTwitterStatus扩展的AsyncTask<虚空,虚空,字符串> {
@覆盖
保护字符串doInBackground(空... PARAMS){
字符串testStatus =PROVA鸣叫;
ConfigurationBuilder CB =新ConfigurationBuilder();
//设置不accesstoken-桌面客户端以下
cb.setDebugEnabled(真)
.setOAuthConsumerKey(******)
.setOAuthConsumerSecret(
*****);
尝试 {
TwitterFactory TF =新TwitterFactory(cb.build());
叽叽喳喳= tf.getInstance();
Log.i(bauu,妙);
requestToken = twitter.getOAuthRequestToken();
串authUrl = requestToken.getAuthenticationURL();
startActivity(新意图(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL())));
的uri = Uri.parse(requestToken.getAuthenticationURL());
返回authUrl;
}赶上(例外五){
e.printStackTrace();
}
返回null;
}
@覆盖
保护无效onPostExecute(String s)将{
startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(S)));
}
}
解决方案
请确保在twitter上开发的应用程序选择你的回调网址如下,
的http://你的-URL /应用程序://你的-APP-HOST
和在你的Android清单文件中的,您可以到Twitter上的actvitiy的, 请确保您定义的:
<意向滤光器>
<作用机器人:名称=android.intent.action.VIEW/>
<类机器人:名称=android.intent.category.DEFAULT/>
<类机器人:名称=android.intent.category.BROWSABLE/>
<数据
机器人:主机=YOUR-APP-HOST
机器人:计划=应用程序/>
&所述; /意图滤光器>
最后,请确保您的程序,
最终公共静态字符串CALLBACK_URL =应用程序://你的-APP-HOST;
I try to write an application where an user can log in on twitter. I use twitter4j like library.My problem is that when I go in the page where I must put username and password, the program block because i don't know use callback to came in my application. Someone can me help?
public class MainActivity extends Activity {
private Twitter twitter;
RequestToken requestToken;
final public static String CALLBACK_SCHEME = "x-latify-oauth-twitter";
final public static String CALLBACK_URL = CALLBACK_SCHEME + "://callback";
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new updateTwitterStatus().execute();
}
});
}
@Override
protected void onDestroy() {
twitter.shutdown();
}
class updateTwitterStatus extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String testStatus = "prova tweet ";
ConfigurationBuilder cb = new ConfigurationBuilder();
// the following is set without accesstoken- desktop client
cb.setDebugEnabled(true)
.setOAuthConsumerKey("******")
.setOAuthConsumerSecret(
"*****");
try {
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
Log.i("bauu", "miao");
requestToken = twitter.getOAuthRequestToken();
String authUrl = requestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL())));
uri = Uri.parse(requestToken.getAuthenticationURL());
return authUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(s)));
}
}
解决方案
Make sure your callback URL in twitter dev app options are as follows,
http://YOUR-URL/app://YOUR-APP-HOST
and within your android manifest file, in between the of the actvitiy that takes you to twitter, make sure you define:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="YOUR-APP-HOST"
android:scheme="app" />
</intent-filter>
lastly, make sure in your program,
final public static String CALLBACK_URL = "app://YOUR-APP-HOST";