0

我正在使用 Scribe API 与 LinkedIn 集成,遇到了我得到 NPE 的问题,代码:

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

    mContext = this;

    mConnections = new ArrayList<String>();

    // List and adapter
    mLinkedInList = (ListView) findViewById(R.id.linkedInList); 
    mLinkedInListAdapter = new LinkedInAdapter(this, mContext, mConnections);         
    mLinkedInList.setAdapter(mLinkedInListAdapter);     

    // Start async task to get connections
    LinkedInConnectionsTask task = new LinkedInConnectionsTask();
    task.execute();
}

异步任务:

 private class LinkedInConnectionsTask extends AsyncTask<Void, Void, String> {

    private String LINKED_IN_CONNECTIONS_API =     "http://api.linkedin.com/v1/people/~/connections";

    @Override
    protected String doInBackground(Void... arg) {

        String urlStr = LINKED_IN_CONNECTIONS_API;
        urlStr += "?format=json";

        SharedPreferences prefs =          PreferenceManager.getDefaultSharedPreferences(mContext);
            String access_token = prefs.getString("linkedin_access_token", null);
            String access_secret = prefs.getString("linkedin_access_secret", null);



        if ( access_token != null && access_secret != null ) {
            OAuthService linkedInService = new ServiceBuilder()
            .provider(LinkedInApi.class)
            .apiKey( LinkedInOAuthActivity.APIKEY )
            .apiSecret( LinkedInOAuthActivity.APISECRET )
            .scope("r_basicprofile")
            .scope("rw_nus")
            .callback( LinkedInOAuthActivity.CALLBACK )
            .build();

            OAuthRequest request = new OAuthRequest(Verb.GET, urlStr);  


            Token t = new Token(access_token, access_secret);
            linkedInService.signRequest(t, request);

            Response response = null;
            try {
                response = request.send();
                if ( response.isSuccessful() )
                    return response.getBody();
                else
                    return null;
            }
            catch ( Exception e ) {
                e.printStackTrace();
                return null;
            }
        }
        else {
            return null;
        }               
    }

    @Override
    protected void onPostExecute(String result) {
        JSONArray arr = null;
        JSONObject object = null; 

        try {
            object = new JSONObject(result);
            if ( object.has("values") ) {
                arr = object.getJSONArray("values");
            }
            else {
                return;
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
            return;
        }

        JSONObject jsonObject = null; 
        for ( int i = 0; i < arr.length(); i++) {
            try {
                jsonObject = arr.getJSONObject(i);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

            try {       
                String fn = jsonObject.getString("firstName");
                String ln = jsonObject.getString("lastName");
                mLinkedInListAdapter.add(fn + " " + ln);
            }
            catch (JSONException e1) {
                e1.printStackTrace();
            }
        }

        mLinkedInListAdapter.notifyDataSetChanged();
    }
}

让 NPE 进入:

response = request.send();

错误日志:

05-22 13:18:33.559: E/AndroidRuntime(4979): FATAL EXCEPTION: main
05-22 13:18:33.559: E/AndroidRuntime(4979): java.lang.NullPointerException
05-22 13:18:33.559: E/AndroidRuntime(4979):     at     

    org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at org.json.JSONTokener.nextValue(JSONTokener.java:94)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at org.json.JSONObject.<init>(JSONObject.java:154)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at org.json.JSONObject.<init>(JSONObject.java:171)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at com.sourcetricks.myconnections.LinkedInListActivity$LinkedInConnectionsTask.onPostExecute(LinkedInListActivity.java:164)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at com.sourcetricks.myconnections.LinkedInListActivity$LinkedInConnectionsTask.onPostExecute(LinkedInListActivity.java:1)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.os.AsyncTask.finish(AsyncTask.java:631)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.os.Looper.loop(Looper.java:137)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at android.app.ActivityThread.main(ActivityThread.java:4898)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at java.lang.reflect.Method.invoke(Method.java:511)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-22 13:18:33.559: E/AndroidRuntime(4979):     at dalvik.system.NativeStart.main(Native Method)

如果有人可以提供帮助,将不胜感激。我做错了什么可能错过了一些东西。谢谢

4

1 回答 1

0

找到了答案。必须在 OAuth 用户协议中启用 r_basicprofile 如果有人需要完整的示例以将 android 与linkedin 集成并获取连接,例如参见教程

于 2013-05-22T12:09:43.290 回答