android – Capture data from basic profile Linkedin

Question:

I'm performing an integration of my app with Linkedin, what happens is that I manage to authenticate and when I return to my app's Activity it shows me the data of the logged in user.

I'm using the Linkedin API codes, but because I'm still starting development I don't know where and how to call the method that retrieves the data, below my code.

Can you help me with this? I just want to know a way to get the first and last name of the logged in user.

Thank you very much in advance.

package com.example.testeli;

import com.linkedin.platform.LISession;
import com.linkedin.platform.LISessionManager;
import com.linkedin.platform.errors.APIHelper;
import com.linkedin.platform.errors.LIApiError;
import com.linkedin.platform.errors.LIAuthError;
import com.linkedin.platform.listeners.ApiListener;
import com.linkedin.platform.listeners.ApiResponse;
import com.linkedin.platform.listeners.AuthListener;
import com.linkedin.platform.utils.Scope;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.testeli.R;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        final Activity thisActivity = this;

        Button linkedin = (Button) findViewById(R.id.linkedin);

        linkedin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LISessionManager.getInstance(getApplicationContext()).init(
                        thisActivity, buildScope(), new AuthListener() {

                            @Override
                            public void onAuthSuccess() {
                                // Authentication was successful. You can now do
                                // other calls with the SDK.

                            }

                            @Override
                            public void onAuthError(LIAuthError error) {
                                // Handle authentication errors
                            }

                        }, true);

            }
        });

    }

    // Build the list of member permissions our LinkedIn session requires
    private static Scope buildScope() {
        return Scope.build(Scope.R_BASICPROFILE, Scope.W_SHARE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Add this line to your existing onActivityResult() method
        LISessionManager.getInstance(getApplicationContext()).onActivityResult(
                this, requestCode, resultCode, data);
    }

    public void getProfile() {
        String url = "https://api.linkedin.com/v1/people/~?format=json:(id,first-name,last-name)";

        APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
        apiHelper.getRequest(this, url, new ApiListener() {
            @Override
            public void onApiSuccess(ApiResponse s) {
                onApiSuccess(s);

            }

            @Override
            public void onApiError(LIApiError LIApiError) {
                // TODO Auto-generated method stub

            }

        });

    }
}

Answer:

I managed to solve my problem, as said above I'm using the linkedin API where there is a class called APIResponse, from what I understand it already performs all the JSON processing.

I hope that if someone faces the same problem the code snippet below can help.

public void getProfile() {
    String url = "https://api.linkedin.com/v1/people/~";

    APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
    apiHelper.getRequest(this, url, new ApiListener() {
        @Override
        public void onApiSuccess(ApiResponse s) {
            // onApiSuccess(s);
            JSONObject json = s.getResponseDataAsJson();
            try {
                String nome = json.getString("firstName");
                String sobrenome = json.getString("lastName");
                // JSONObject nome = json.getJSONObject("lastName");
                // nome.toString();

                System.out.println(nome);
                System.out.println(sobrenome);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        @Override
        public void onApiError(LIApiError LIApiError) {
            // TODO Auto-generated method stub

        }

    });

}
Scroll to Top
AllEscort