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

Android Linkedin:oauth.signpost.exception.OAuthCommunicationException:与服务提供商的通信失败:null,android,exception,null,linkedin,provider,Android,Exception,Null,Linkedin,Provider,我将在我的android应用程序中访问linkedin个人资料。我使用了下面的代码,它在android设备上运行良好,但在emulator上崩溃了。不知道问题出在哪里。我的代码如下 Config.java public class Config { public static String LINKEDIN_CONSUMER_KEY = "75rbbjsc94argh"; public static String LINKEDIN_CONSUMER_SECRET = "G85W

我将在我的android应用程序中访问linkedin个人资料。我使用了下面的代码,它在android设备上运行良好,但在emulator上崩溃了。不知道问题出在哪里。我的代码如下

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);
    }
}
公共类LinkedinDialog扩展对话框{
private ProgressDialog ProgressDialog=null;
公共静态链接APIClientFactory工厂;
公共静态链接oAuthService oAuthService;
公共静态LinkedInRequestToken-liToken;
//构建一个新的LinkedIn对话框
公共LinkedinDialog(上下文上下文,ProgressDialog ProgressDialog){
超级(上下文);
this.progressDialog=progressDialog;
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);//必须在超级之前调用。
super.onCreate(savedInstanceState);
setContentView(R.layout.ln_对话框);
setWebView();
}
//设置webview。
私有void setWebView(){
LinkedinDialog.oAuthService=LinkedInOAuthServiceFactory.getInstance()
.createLinkedInOAuthService(Config.LINKEDIN_CONSUMER_密钥,
配置LINKEDIN_消费者_机密);
LinkedinDialog.factory=LinkedInApiClientFactory.newInstance(
Config.LINKEDIN_CONSUMER_KEY,Config.LINKEDIN_CONSUMER_SECRET);
LinkedinDialog.liToken=LinkedinDialog.oAuthService.getOAuthRequestToken(Config.OAUTH_回调_URL);
WebView mWebView=(WebView)findViewById(R.id.webkitWebView1);
mWebView.getSettings().setJavaScriptEnabled(true);
Log.i(“LinkedinSample”,LinkedinDialog.liToken.getAuthorizationUrl());
mWebView.loadUrl(LinkedinDialog.liToken.getAuthorizationUrl());
setWebViewClient(新的HelloWebViewClient());
mWebView.setPictureListener(新PictureListener(){
@凌驾
公共void onNewPicture(网络视图、图片){
if(progressDialog!=null&&progressDialog.isShowing()){
progressDialog.disclose();
}
}
});
}
//用于内部url加载的webview客户端
类HelloWebViewClient扩展了WebViewClient{
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
if(url.contains(Config.OAUTH\u CALLBACK\u url)){
Uri=Uri.parse(url);
字符串验证器=uri.getQueryParameter(“oauth_验证器”);
取消();
for(OnVerifyListener d:listeners){
//调用侦听器方法
d、 onVerify(验证者);
}
}else if(url.contains()https://www.linkedin.com/uas/oauth/redorangetechnologies.com")) {
取消();
}否则{
Log.i(“LinkedinSample”,“url:+url”);
view.loadUrl(url);
}
返回true;
}
}
/**
*侦听器列表。
*/
私有列表侦听器=新的ArrayList();
/**
*注册要在身份验证完成时调用的回调。
* 
*@param数据
*将运行的回调
*/
public void setVerifierListener(OnVerifyListener数据){
添加(数据);
}
/**
*oauth_验证程序的侦听器。
*/
接口OnVerifyListener{
/**
*身份验证完成时调用。
* 
*@param验证器
*oauth_验证程序代码。
*/
public void onVerify(字符串验证器);
}
}
应用程序崩溃,单击登录按钮,setWebView()上显示错误;在LinkedinDialog类中

以下是日志:

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)
01-17 15:50:06.513:E/AndroidRuntime(2544):致命异常:主
01-17 15:50:06.513:E/AndroidRuntime(2544):com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceException:oauth.signpost.exception.OAuthCommunicationException:与服务提供商的通信失败:null
01-17 15:50:06.513:E/AndroidRuntime(2544):位于com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceImpl.getOAuthRequestToken(LinkedInOAuthServiceImpl.java:205)
01-17 15:50:06.513:E/AndroidRuntime(2544):位于com.mukesh.linkedin.LinkedinDialog.setWebView(LinkedinDialog.java:65)
01-17 15:50:06.513:E/AndroidRuntime(2544):位于com.mukesh.linkedin.LinkedinDialog.onCreate(LinkedinDialog.java:54)
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.app.Dialog.dispatchOnCreate(Dialog.java:340)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.app.Dialog.show(Dialog.java:253)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在com.mukesh.linkedin.LinkedInSampleActivity.linkedInLogin(LinkedInSampleActivity.java:100)
01-17 15:50:06.513:E/AndroidRuntime(2544):在com.mukesh.linkedin.LinkedInSampleActivity.access$0(LinkedInSampleActivity.java:94)
01-17 15:50:06.513:E/AndroidRuntime(2544):在com.mukesh.linkedin.LinkedInSampleActivity$1.onClick(LinkedInSampleActivity.java:69)
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.view.view.performClick(view.java:3480)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.view.view$PerformClick.run(view.java:13983)
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.os.Handler.handleCallback(Handler.java:605)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.os.Handler.dispatchMessage(Handler.java:92)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.os.Looper.loop(Looper.java:137)上
01-17 15:50:06.513:E/AndroidRuntime(2544):在android.app.ActivityThread.main(ActivityThread.java:4340)上
01-17 15:50:06.513:E/AndroidRuntime(2544):位于java.lang.reflect.Method.Invokenactive(本机方法)
01-17 15:50:06.5
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
        }