2013-03-23 50 views
1

我使用Twitter4j来制作一个Twitter应用程序,用于学习如何制作应用程序。我通过复制sample code from the twitter4j site为ICS制作了一个非常基本的应用程序。 twitter4j api发送OAuth进程的http请求,然后打开授权过程发生的网页。问题是应用程序卡在代码的http请求部分。我已在清单文件中添加了许可<uses-permission android:name="android.permission.INTERNET"/>。代码片段如下。使用Twitter4j的Http请求不起作用并卡住

 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); 
         } 

我敢肯定的应用卡在RequestToken tempToken = temp.getOAuthRequestToken();线监守我没有收到,要求我选择浏览器的提示。 twitter4j网站的示例代码正常工作。我通过在java应用程序中使用它进行验证。

为什么会有这种情况发生?我还必须包含其他许可以及返回请求吗?

+0

如果您在IDE中执行此操作,请尝试设置断点并点击它。 – karmanaut 2013-03-29 13:23:37

回答

1

这是通过执行http请求来为异步任务上的twitter4j检索请求令牌来解决的。

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()); 
         } 

        }