1

I have been trying different things all day, but I still can't parse this JSON response. I successfully got a response from LinkedIn (I know, because I logged it), and now I'm trying to extract values from it, but the code I am using is creating an error saying there is no value of firstName. My 2nd log statement won't print. My 1st log statement, the response, is printed below at the bottom. What am I doing wrong?

My class where I call the API

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) {

    }
}

Error from Logcat

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)

One of my log statements, the response

05-08 18:38:45.577 7028-7028/org.azurespot.awesomesde I/LINKEDIN RESPONSE:: {"StatusCode":200,"responseData":"{\n  \"firstName\": \"AFirstName\",\n  \"id\": \"hZvMRB-3Yg\",\n  \"lastName\": \"ALastName\"\n}","Location":""}
Azurespot
  • 3,066
  • 3
  • 45
  • 73

1 Answers1

0

Great, thanks to @cricket_007, I was able to trace how to solve this. I have a nested JSON file as a response, which means I must get the first level first, which is the responseData key, and extract that into another new JSONObject, and then from there, I can extract the first and last name. Here's the working code 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();
        }

    }
Azurespot
  • 3,066
  • 3
  • 45
  • 73