2

Currently i need to make an application that can list all of Google Drive file. i already did the account choosing and oauth process, an already get the token. but when i try to use API call to list all my file on Google Drive (By using drive.files.list) i didn't get any result, the arraylist of files which is supposed to hold all the file is still empty.
i also got error :

java.net.unknownHostException www.googleapis.com cannot be resolved


this is my code :

SharedPreferences settings = getSharedPreferences(PREF, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("accountName", got.name);
    editor.commit();
    account=got;
    amf=accMgr.getAuthToken(account, authTokenType, true,
                new AccountManagerCallback<Bundle>(){
                    public void run(AccountManagerFuture<Bundle> arg0) {
                        try {
                            Bundle result;
                            Intent i;
                            String token;
                            Drive a;
                            result = arg0.getResult();
                            if (result.containsKey(accMgr.KEY_INTENT)) {
                                i = (Intent)result.get(accMgr.KEY_INTENT);
                                if (i.toString().contains("GrantCredentialsPermissionActivity")) {
                                    // Will have to wait for the user to accept
                                    // the request therefore this will have to
                                    // run in a foreground application
                                    cbt.startActivity(i);
                                } else {
                                    cbt.startActivity(i);
                                }

                            }
                            else if (result.containsKey(accMgr.KEY_AUTHTOKEN)) {
                                accessProtectedResource.setAccessToken(result
                                        .getString(accMgr.KEY_AUTHTOKEN));
                                   buildService(result
                                        .getString(accMgr.KEY_AUTHTOKEN),API_KEY);
                            /*else {
                                token = (String)result.get(AccountManager.KEY_AUTHTOKEN);*/

                                /*
                                 * work with token
                                 */

                                // Remember to invalidate the token if the web service rejects it
                                // if(response.isTokenInvalid()){
                                //    accMgr.invalidateAuthToken(authTokenType, token);
                                // }

                            }
                        } catch (OperationCanceledException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (AuthenticatorException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                }, handler);

}

private void buildService(final String authToken, final String ApiKey) {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
    b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
        @Override
        public void initialize(JsonHttpRequest request) throws IOException {
            DriveRequest driveRequest = (DriveRequest) request;
            driveRequest.setPrettyPrint(true);
            driveRequest.setKey(ApiKey);
            driveRequest.setOauthToken(authToken);
        }
    });
    System.out.println(authToken);
    service= b.build();
    List<File> a=new ArrayList<File>();
    try {
        a = retrieveDriveFile(service);
        System.out.println(a.size());
        File c=a.get(0);
        TextView ad=(TextView) findViewById(R.id.test);
        ad.setText(c.getOriginalFilename());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public List<File> retrieveDriveFile(Drive service) throws IOException{
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error ssoccurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    return result;
  }
KevinL
  • 83
  • 2
  • 10

2 Answers2

0

This would typically happen if you don't have a working internet connection on your device.

Also don't forget to add the following permission:

<uses-permission android:name="android.permission.INTERNET" />

This could happen also if you are behind a proxy. If that's the case please have a look at this question.

Community
  • 1
  • 1
Nicolas Garnier
  • 12,134
  • 2
  • 42
  • 39
  • Thank you :D your solution work like miracle for my app. i just add the internet permission on my app. – KevinL Aug 21 '12 at 23:09
0

If you want to get all the file from Goolge Drive. Assume that you have already done the Account Choosing process and creating Drive (Drive mService) etc. Now Under Button Click Event call this function

 ButtonClickEvent
  {
     GetDriveData();
  }
        // FUNCTION TO RETRIEVE GOOGLE DRIVE DATA
 private void GetDriveData() 
    {
       private List<File>   mResultList;                
      Thread t = new Thread(new Runnable() 
    {
        @Override
        public void run() 
        {
              mResultList = new ArrayList<File>();
            com.google.api.services.drive.Drive.Files f1 = mService.files();
            com.google.api.services.drive.Drive.Files.List request = null;

            do 
            {
                try 
                { 
                    request = f1.list();
                    request.setQ("trashed=false");
                    com.google.api.services.drive.model.FileList fileList = request.execute();
                    mResultList.addAll(fileList.getItems());

                }
                catch (UserRecoverableAuthIOException e)
                {
                    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } 
                catch (IOException e)
                {
                    e.printStackTrace();
                    if (request != null)
                    {
                        request.setPageToken(null);
                    }
                }
            } while (request.getPageToken() !=null && request.getPageToken().length() > 0);

            populateListView();//Calling to Populate Data to the List
        }

    });
    t.start();
}

 //Populating Retrieved data to List
 private void populateListView() 
    {
        runOnUiThread(new Runnable() 
            {
                @Override
                public void run() 
                {

                    mFileArray = new String[mResultList.size()];
                    int i = 0;
                    for(File tmp : mResultList)
                    {
                        //System.out.println("FILE DATA "+tmp.getId()+"."+tmp.getFileSize()+".."+tmp.getFileExtension()+",,"+tmp.getMimeType()+"/"+tmp.getTitle());
                        mFileArray[i] = tmp.getTitle();
                    i++;
                    }

                    mAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, mFileArray);
                mListView.setAdapter(mAdapter);
                    button2.setText("yes");
                }
            });
        }
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81