我一直在尝试不同的事情,但我仍然无法解析这个JSON响应。我成功地从LinkedIn获得了回复(我知道,因为我记录了它),现在我试图从中提取值,但我正在使用的代码创建了一个错误,表示没有值{{1 }}。我的第二个日志声明不会打印出来。我的第一个日志声明,响应,打印在底部的下方。我做错了什么?
我称之为API的课程
firstName
Logcat错误
package org.azurespot.awesomesde;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.linkedin.platform.APIHelper;
import com.linkedin.platform.errors.LIApiError;
import com.linkedin.platform.listeners.ApiListener;
import com.linkedin.platform.listeners.ApiResponse;
import org.json.JSONException;
import org.json.JSONObject;
public class LinkedInLoggedInActivity extends AppCompatActivity {
String urlLinkedIn = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name)?format=json";
JSONObject results;
TextView firstName;
TextView lastName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linked_in_logged_in);
firstName = (TextView)findViewById(R.id.textViewFirst);
lastName = (TextView)findViewById(R.id.textViewLast);
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.getRequest(this, urlLinkedIn, new ApiListener() {
@Override
public void onApiSuccess(ApiResponse apiResponse) {
// Success!
Log.i("LINKEDIN RESPONSE: ", apiResponse.toString()); // 1st log
try {
results = new JSONObject(apiResponse.toString());
Log.i("NAME FROM LINKED IN: ", results.get("firstName").toString()); // 2nd log
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onApiError(LIApiError liApiError) {
// Error making GET request!
liApiError.printStackTrace();
}
});
}
// get first name
public void linkedInFirst(View v){
try {
firstName.setText(results.get("firstName").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
// get last name
public void linkedInLast(View v) {
}
}
我的一个日志声明,响应
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: org.json.JSONException: No value for firstName
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at org.azurespot.awesomesde.LinkedInLoggedInActivity$1.onApiSuccess(LinkedInLoggedInActivity.java:42)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.linkedin.platform.APIHelper$1.onResponse(APIHelper.java:99)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.linkedin.platform.APIHelper$1.onResponse(APIHelper.java:95)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at android.os.Looper.loop(Looper.java:145)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5835)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
05-08 18:58:38.227 18345-18345/org.azurespot.awesomesde W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:0)
太好了,感谢@ cricket_007,我能够追踪如何解决这个问题。我有一个嵌套的JSON文件作为响应,这意味着我必须首先获得第一个级别,即responseData
密钥,并将其提取到另一个新的JSONObject
,然后从那里,我可以提取名字和姓氏。这是工作代码snipets:
JSONObject results;
JSONObject resultsInner;
TextView firstName;
TextView lastName;
@Override
public void onApiSuccess(ApiResponse apiResponse) {
// Success!
Log.i("LINKEDIN RESPONSE: ", apiResponse.toString());
try {
results = new JSONObject(apiResponse.toString());
// must get the key responseData first, then will extract values from it
resultsInner = new JSONObject(results.getString("responseData"));
} catch (JSONException e) {
e.printStackTrace();
}
}
...
// get first name
public void linkedInFirst(View v){
try {
firstName.setText(resultsInner.getString("firstName"));
} catch (JSONException e) {
e.printStackTrace();
}
}
// get last name
public void linkedInLast(View v) {
try {
lastName.setText(resultsInner.getString("lastName"));
} catch (JSONException e) {
e.printStackTrace();
}
}