Linkedin:oauth.signpost.exception.OAuthCommunicationException:与服务提供者的通信失败:null

时间:2014-01-17 10:22:07

标签: android exception null linkedin provider

我要在我的Android应用中访问linkedin个人资料。我使用下面的代码,它在Android设备上工作正常,但在模拟器上崩溃。不知道问题出在哪里。我的代码如下。

Config.java

public class Config {

    public static String LINKEDIN_CONSUMER_KEY = "75rbbjsc94argh";
    public static String LINKEDIN_CONSUMER_SECRET = "G85WTKgcIijk4U6Yu";
    public static String scopeParams = "rw_nus+r_basicprofile+r_fullprofile+r_emailaddress+r_network+r_contactinfo+rw_groups+rw_company_admin";

    public static String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
    public static String OAUTH_CALLBACK_HOST = "callback";
    //public static String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
    public static String OAUTH_CALLBACK_URL = "x-oauthflow-linkedin://callback";
}

LinkedInSampleActivity.java

public class LinkedInSampleActivity extends Activity {
    Button login;
    Button share;
    EditText et;
    TextView name;
    ImageView photo;
    public static final String OAUTH_CALLBACK_HOST = "litestcalback";

    final LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory
            .getInstance().createLinkedInOAuthService(
                    Config.LINKEDIN_CONSUMER_KEY,Config.LINKEDIN_CONSUMER_SECRET, Config.scopeParams);
    final LinkedInApiClientFactory factory = LinkedInApiClientFactory
            .newInstance(Config.LINKEDIN_CONSUMER_KEY,
                    Config.LINKEDIN_CONSUMER_SECRET);
    LinkedInRequestToken liToken;
    LinkedInApiClient client;
    LinkedInAccessToken accessToken = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        share = (Button) findViewById(R.id.share);
        name = (TextView) findViewById(R.id.name);
        et = (EditText) findViewById(R.id.et_share);
        login = (Button) findViewById(R.id.login);
        photo = (ImageView) findViewById(R.id.photo);

        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                linkedInLogin();
            }
        });
    }

    private void linkedInLogin() {
        ProgressDialog progressDialog = new ProgressDialog(
                LinkedInSampleActivity.this);

        LinkedinDialog d = new LinkedinDialog(LinkedInSampleActivity.this,
                progressDialog);
        d.show();

        // set call back listener to get oauth_verifier value
        d.setVerifierListener(new OnVerifyListener() {
            @Override
            public void onVerify(String verifier) {
                try {
                    Log.i("LinkedinSample", "verifier: " + verifier);

                    accessToken = LinkedinDialog.oAuthService
                            .getOAuthAccessToken(LinkedinDialog.liToken,
                                    verifier);
                    LinkedinDialog.factory.createLinkedInApiClient(accessToken);
                    client = factory.createLinkedInApiClient(accessToken);


                    client.postNetworkUpdate("Testing by Umesh!!! LinkedIn wall post from Android app");


                    Person p2 = client.getProfileForCurrentUser(EnumSet.of(ProfileField.ID)); 
                    Person p = client.getProfileById(p2.getId(), EnumSet.of(
                    ProfileField.FIRST_NAME,
                    ProfileField.LAST_NAME,
                    ProfileField.SKILLS,
                    ProfileField.HEADLINE,
                    ProfileField.SUMMARY,
                    ProfileField.LANGUAGES,
                    ProfileField.LANGUAGES_LANGUAGE_NAME,
                    ProfileField.LANGUAGES_PROFICIENCY_NAME,
                    ProfileField.LANGUAGES_LANGUAGE,
                    ProfileField.HONORS,
                    ProfileField.INTERESTS,
                    ProfileField.POSITIONS,
                    ProfileField.EDUCATIONS,
                    ProfileField.INDUSTRY,
                    ProfileField.API_STANDARD_PROFILE_REQUEST,
                    ProfileField.PICTURE_URL,
                    ProfileField.PUBLIC_PROFILE_URL));

                    name.setText("Welcome " + p.getFirstName() + " "+ p.getLastName());

                    Log.i("LinkedinSample","ln_access_token: " + accessToken.getToken());
                    Log.i("LinkedinSample","ln_access_token: " + accessToken.getTokenSecret());
                    Log.i("FirstName"," : " + p.getFirstName());
                    Log.i("LastName"," : " + p.getLastName());
                    Log.i("User Summary"," : " + p.getSummary());
                    Log.i("Headline"," : " + p.getHeadline());
                    Log.i("Interest"," : "+p.getInterests());

                    //----------------------------------------------------

                    for (Position position:p.getPositions().getPositionList())
                    {
                        Log.i("position "," : " + position.getTitle());
                        Log.i("comp "," : " + position.getCompany().getName());

                    }

                    for (Education education:p.getEducations().getEducationList()) 
                    {
                        Log.i("Degree "," : "+ education.getDegree());
                        Log.i("School name "," : "+ education.getSchoolName());

                    }
                    Log.i("Language "," : ");
                    for(Language language:p.getLanguages().getLanguageList())
                    {
                        Log.i(""," : "+language.getLanguage().getName());
                    }
                    Log.i("Skill "," : ");
                    for(Skill skill:p.getSkills().getSkillList())
                    {
                        Log.i(""," : "+skill.getSkill().getName());
                    }
                    Log.i("Honor"," : "+p.getHonors());
                    //----------------------------------------------------

                    name.setVisibility(0);
                    login.setVisibility(4);
                    share.setVisibility(0);
                    et.setVisibility(0);

                } catch (Exception e) {
                    Log.i("LinkedinSample", "error to get verifier");
                    e.printStackTrace();
                }
            }
        });

        // set progress dialog
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(true);
        progressDialog.show();
    }
}

LinkedinDialog.java

public class LinkedinDialog extends Dialog {
    private ProgressDialog progressDialog = null;

    public static LinkedInApiClientFactory factory;
    public static LinkedInOAuthService oAuthService;
    public static LinkedInRequestToken liToken;

    //Construct a new LinkedIn dialog

    public LinkedinDialog(Context context, ProgressDialog progressDialog) {
        super(context);
        this.progressDialog = progressDialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);// must call before super.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ln_dialog);
        setWebView();
    }

    // set webview.
    private void setWebView() {
        LinkedinDialog.oAuthService = LinkedInOAuthServiceFactory.getInstance()
                .createLinkedInOAuthService(Config.LINKEDIN_CONSUMER_KEY,
                        Config.LINKEDIN_CONSUMER_SECRET);
        LinkedinDialog.factory = LinkedInApiClientFactory.newInstance(
                Config.LINKEDIN_CONSUMER_KEY, Config.LINKEDIN_CONSUMER_SECRET);

        LinkedinDialog.liToken = LinkedinDialog.oAuthService.getOAuthRequestToken(Config.OAUTH_CALLBACK_URL);

        WebView mWebView = (WebView) findViewById(R.id.webkitWebView1);
        mWebView.getSettings().setJavaScriptEnabled(true);

        Log.i("LinkedinSample", LinkedinDialog.liToken.getAuthorizationUrl());
        mWebView.loadUrl(LinkedinDialog.liToken.getAuthorizationUrl());
        mWebView.setWebViewClient(new HelloWebViewClient());

        mWebView.setPictureListener(new PictureListener() {
            @Override
            public void onNewPicture(WebView view, Picture picture) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }

            }
        });

    }

    //webview client for internal url loading

    class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(Config.OAUTH_CALLBACK_URL)) {
                Uri uri = Uri.parse(url);
                String verifier = uri.getQueryParameter("oauth_verifier");
                cancel();
                for (OnVerifyListener d : listeners) {
                    // call listener method
                    d.onVerify(verifier);
                }
            } else if (url.contains("https://www.linkedin.com/uas/oauth/redorangetechnologies.com")) {
                cancel();
            } else {
                Log.i("LinkedinSample", "url: " + url);
                view.loadUrl(url);
            }

            return true;
        }
    }

    /**
     * List of listener.
     */
    private List<OnVerifyListener> listeners = new ArrayList<OnVerifyListener>();

    /**
     * Register a callback to be invoked when authentication have finished.
     * 
     * @param data
     *            The callback that will run
     */
    public void setVerifierListener(OnVerifyListener data) {
        listeners.add(data);
    }

    /**
     * Listener for oauth_verifier.
     */
    interface OnVerifyListener {
        /**
         * invoked when authentication have finished.
         * 
         * @param verifier
         *            oauth_verifier code.
         */
        public void onVerify(String verifier);
    }
}

应用程序崩溃,单击登录按钮,错误显示在setWebView();在LinkedinDialog类中。

以下是logcat:

01-17 15:50:06.513: E/AndroidRuntime(2544): FATAL EXCEPTION: main
01-17 15:50:06.513: E/AndroidRuntime(2544): com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:205)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.mukesh.linkedin.LinkedinDialog.setWebView(LinkedinDialog.java:65)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.mukesh.linkedin.LinkedinDialog.onCreate(LinkedinDialog.java:54)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.app.Dialog.dispatchOnCreate(Dialog.java:340)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.app.Dialog.show(Dialog.java:253)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.mukesh.linkedin.LinkedInSampleActivity.linkedInLogin(LinkedInSampleActivity.java:100)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.mukesh.linkedin.LinkedInSampleActivity.access$0(LinkedInSampleActivity.java:94)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.mukesh.linkedin.LinkedInSampleActivity$1.onClick(LinkedInSampleActivity.java:69)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.view.View.performClick(View.java:3480)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.view.View$PerformClick.run(View.java:13983)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.os.Handler.handleCallback(Handler.java:605)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.os.Looper.loop(Looper.java:137)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.app.ActivityThread.main(ActivityThread.java:4340)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at java.lang.reflect.Method.invoke(Method.java:511)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at dalvik.system.NativeStart.main(Native Method)
01-17 15:50:06.513: E/AndroidRuntime(2544): Caused by: oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: null
01-17 15:50:06.513: E/AndroidRuntime(2544):     at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:196)
01-17 15:50:06.513: E/AndroidRuntime(2544):     ... 18 more
01-17 15:50:06.513: E/AndroidRuntime(2544): Caused by: android.os.NetworkOnMainThreadException
01-17 15:50:06.513: E/AndroidRuntime(2544):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:164)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at oauth.signpost.basic.DefaultOAuthProvider.sendRequest(DefaultOAuthProvider.java:48)
01-17 15:50:06.513: E/AndroidRuntime(2544):     at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:177)

3 个答案:

答案 0 :(得分:10)

在onCreate()中的setContentView()之后添加以下函数。

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
        }

这对我有用。希望它能为你效劳。

答案 1 :(得分:2)

顺便说一下

将AsyncTask用于后台任务会更好。

答案 2 :(得分:0)

除严格模式政策外,您还可以正确设置移动日期和时间