私は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)
誰かが助けてくれれば幸いです。私はおそらく何かが欠けている何か間違ったことをしています。ありがとう