1

我正在使用Twitter4j制作一个 twitter 应用程序来学习如何制作应用程序。通过从 twitter4j 站点复制示例代码,我为 ICS 制作了一个非常基本的应用程序。twitter4j api 为 OAuth 进程发送一个 http 请求,然后打开一个发生授权过程的网页。问题是应用程序卡在代码的 http 请求部分。我已经<uses-permission android:name="android.permission.INTERNET"/>在清单文件中添加了权限。代码片段如下。

        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 应用程序中使用它来验证这一点。

发生这种情况有什么原因吗?我是否还必须包括其他一些许可才能返回请求?

4

1 回答 1

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

                    }
于 2013-03-30T18:05:01.327 回答