Reputation: 347
I'm using Twitter4j for making a twitter app for learning how to make apps. I've made a very basic app for ICS by copying the sample code from the twitter4j site. The twitter4j api sends an http request for OAuth process and then opens a webpage where the authorization process happens. The problem is the app gets stuck in the http request part of the code. I have added the permission <uses-permission android:name="android.permission.INTERNET"/>
in the manifest file. Code snippet is as below.
Log.v("storeCredentials", "Started");
System.out.println("storeCredentials"+" Started");
Twitter t1 = TwitterFactory.getSingleton();
t1.setOAuthConsumer(twitter.getConsumerKey(), twitter.getConsumerSecret());
Log.v("storeCredentials", "consumer set");
System.out.println("storeCredentials"+" consumer set");
//ConnectionDetector detective = new ConnectionDetector(this);
System.out.println("storeCredentials"+" debug 5");
Log.v("storeCredentials", "connection present");
//Progress Dialog
System.out.println("storeCredentials"+" debug 6");
final ProgressDialog d1 = new ProgressDialog(this);
System.out.println("storeCredentials"+" debug 7");
d1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
d1.setCancelable(false);
d1.show();
new Thread(new Runnable(){
public void run()
{
Twitter temp = TwitterFactory.getSingleton();
temp.setOAuthConsumer(twitter.getConsumerKey(), twitter.getConsumerSecret());
try
{
RequestToken tempToken = temp.getOAuthRequestToken();
AccessToken accessToken = null;
System.out.println("storeCredentials"+" debug 10");
Log.v("storeCredentials", "tokens init");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//while (null == accessToken) return true;
String url = tempToken.getAuthorizationURL();
//String url = "http://www.google.co.in";
if (!url.startsWith("https://") && !url.startsWith("http://"))
{
url = "http://" + url;
}
Uri webpage = Uri.parse(url);
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
Log.v("storeCredentials", "Intent created");
System.out.println("storeCredentials"+" Intent created");
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(webIntent, 0);
boolean isIntentSafe = activities.size() > 0;
Log.v("Number of browsers", String.valueOf(activities.size()));
System.out.println("storeCredentials"+" Number of browsers" + " String.valueOf(activities.size())");
// Start an activity if it's safe
if (isIntentSafe)
{
startActivity(webIntent);
}
I'm pretty sure the app gets stuck in the RequestToken tempToken = temp.getOAuthRequestToken();
line becuase I'm not getting a prompt that asks me to choose a browser. The sample code from the twitter4j site works fine. I verified this by using it in a java application.
Is there some reason why this happens? Do I have to include some other permission as well for the request to return?
Upvotes: 1
Views: 483
Reputation: 347
This was solved by executing the http request to retrieve a request token for twitter4j on an async task.
public void getRequestToken()
{
Log.v("storeCredentials", "Started");
System.out.println("storeCredentials"+" Started");
Twitter t1 = TwitterFactory.getSingleton();
t1.setOAuthConsumer(twitter.getConsumerKey(), twitter.getConsumerSecret());
Log.v("storeCredentials", "consumer set");
System.out.println("storeCredentials"+" consumer set");
//ConnectionDetector detective = new ConnectionDetector(this);
System.out.println("storeCredentials"+" debug 5");
Log.v("storeCredentials", "connection present");
//Progress Dialog
System.out.println("storeCredentials"+" debug 6");
final ProgressDialog d1 = new ProgressDialog(this);
System.out.println("storeCredentials"+" debug 7");
d1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
d1.setCancelable(false);
d1.show();
new RequestLoginToken().execute(1);
}
public class RequestLoginToken extends AsyncTask<Integer, Void, RequestToken> {
@Override
protected RequestToken doInBackground(Integer... params) {
int code = params[0].intValue();
Twitter temp = TwitterFactory.getSingleton();
System.out.println("storeCredentials"+" debug 7");
RequestToken tempToken = null;
System.out.println("storeCredentials"+" debug 8");
try
{
tempToken = temp.getOAuthRequestToken();
System.out.println("storeCredentials"+" debug 9");
}
catch( Exception e)
{
System.out.println("Error getting token");
}
return tempToken;
}
@Override
protected void onPostExecute(RequestToken token)
{
pd1.dismiss();
if(token==null)
{
Toast.makeText(context, "Try again", Toast.LENGTH_SHORT).show();
return;
}
requestToken = token;
pd1.dismiss();
flag = true;
launchURL(requestToken.getAuthenticationURL());
}
}
Upvotes: 1