Med Amini
Med Amini

Reputation: 35

Can't get the list of folders in Google Drive fom android application

I'm trying to get the list of folders inside my google drive, I have registered my application succesfully, and the code is running till onConnected() method, This is the code I used:

public class MainActivity extends Activity implements
ConnectionCallbacks, OnConnectionFailedListener {
    Button btn1,btn2,btn_index;
    EditText etxt1;
    private GoogleApiClient mGoogleApiClient;
    public TextView tvHttp,etxt2;
    private ListView mResultsListView;
    private ResultsAdapter mResultsAdapter;
    //https://drive.google.com/folderview?id=0B42Bn9gPjvDHd3dvVE1sZGRQWjg&usp=sharing
    public static String EXISTING_FOLDER_ID = "0B42Bn9gPjvDHd3dvVE1sZGRQWjg";

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


        ///==============================================
        if (mGoogleApiClient == null) {
         mGoogleApiClient = new GoogleApiClient.Builder(this)
         .addApi(Plus.API)
         .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
        }
       //================================================= 


       btn1=(Button)findViewById(R.id.button1);
       btn2=(Button)findViewById(R.id.button2);
       btn_index=(Button)findViewById(R.id.button3);
       etxt1=(EditText)findViewById(R.id.editText1);
       etxt2=(TextView)findViewById(R.id.TextView01);
       tvHttp = (TextView) findViewById(R.id.textView1);





        protected void onStart() {
            super.onStart();
            mGoogleApiClient.connect();
        }


        @Override
        public void onConnectionFailed(ConnectionResult arg0) {
            // TODO Auto-generated method stub
            etxt2.setText("connection failed !!!");

        }

        @Override
        public void onConnected(Bundle arg0) {
            super.onCreate(arg0);
            // TODO Auto-generated method stub
            etxt2.setText("connection success !!!");

                setContentView(R.layout.activity_main);
                mResultsListView = (ListView) findViewById(R.id.listViewResults);
                mResultsAdapter = new ResultsAdapter(this);
                mResultsListView.setAdapter(mResultsAdapter);


                Drive.DriveApi.fetchDriveId(mGoogleApiClient, EXISTING_FOLDER_ID)
                .setResultCallback(idCallback);

        }


        final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
            @Override
            public void onResult(DriveIdResult result) {
                if (!result.getStatus().isSuccess()) {
                    //showMessage("Cannot find DriveId. Are you authorized to view this file?");
                    return;
                }
                DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, result.getDriveId());
                folder.listChildren(mGoogleApiClient)
                        .setResultCallback(metadataResult);
            }
        };

        final private ResultCallback<MetadataBufferResult> metadataResult = new
                ResultCallback<MetadataBufferResult>() {
            @Override
            public void onResult(MetadataBufferResult result) {
                if (!result.getStatus().isSuccess()) {
                    //showMessage("Problem while retrieving files");
                    return;
                }
                mResultsAdapter.clear();
                mResultsAdapter.append(result.getMetadataBuffer());
                //showMessage("Successfully listed files.");
            }
        };

    }

If I execute just the first line in onConnected Method, etxt2.setText("connection success !!!");, I have the message : "connection success !!!", which means that the link is successful, but when I try to execute the rest to get the name of the folders I get this error:

05-16 11:49:24.553: E/AndroidRuntime(3782): FATAL EXCEPTION: main
05-16 11:49:24.553: E/AndroidRuntime(3782): Process: com.example.first_app, PID: 3782
05-16 11:49:24.553: E/AndroidRuntime(3782): java.lang.NullPointerException: Appropriate Api was not requested.
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.er.b(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.a(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.a(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.b(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.a(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.drive.internal.l.fetchDriveId(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.example.first_app.MainActivity.onConnected(MainActivity.java:293)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.ei.b(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.dy(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b.d(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.common.api.b$2.onConnected(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.ei.b(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.ei.bo(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.eh$h.b(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.eh$h.a(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.eh$b.ec(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.google.android.gms.internal.eh$a.handleMessage(Unknown Source)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at android.os.Looper.loop(Looper.java:136)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at java.lang.reflect.Method.invoke(Method.java:515)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 11:49:24.553: E/AndroidRuntime(3782):     at dalvik.system.NativeStart.main(Native Method)

any help will be appreciated, thanks in advance.

Upvotes: 0

Views: 354

Answers (2)

AouledIssa
AouledIssa

Reputation: 2814

if (mGoogleApiClient == null) {
     mGoogleApiClient = new GoogleApiClient.Builder(this)
     .addApi(Drive.API)
     .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    }

you should change this : addApi(Plus.API) to this addApi(Drive.API)

Upvotes: 0

Cheryl Simon
Cheryl Simon

Reputation: 46844

As the error message indicates "Appropriate Api was not requested". You are not requesting the correct API in your GoogleApiClient. You need to include Drive.API

Upvotes: 3

Related Questions