我正在制作基于Android google驱动程序演示的代码。我认为我已经复制了所有的setings,因为我可以创建文件和文件夹并进行查询;但是,当我提交doenload文件时,我收到错误消息,必须提供Id。
我的代码如何运作:
在第一,我正在查询以获取我感兴趣的名称id's
的文件。并得到以下:
04-21 11:36:51.374: D/folder id(6468): DriveId:CAESHDBCdzhlOWswMlprblVNRXN4VUd0Nk5IQXphRmsYhgggmNuW1qxR
04-21 11:36:51.374: D/folder name(6468): gdGatheredText.txt
04-21 11:36:51.374: D/folder id(6468): DriveId:CAESHDBCdzhlOWswMlprblVkamR1VFhwNGNtcG9NbGsY_gcgmNuW1qxR
04-21 11:36:51.374: D/folder name(6468): gdGatheredText.txt
04-21 11:36:51.374: D/folder id(6468): DriveId:CAESHDBCdzhlOWswMlprblVlWE5SWm10T1RXWXhZalEY9AcgmNuW1qxR
04-21 11:36:51.374: D/folder name(6468): gdGatheredText.txt
我获取最后一个文件CAESHDBCdzhlOWswMlprblVlWE5SWm10T1RXWXhZalEY9AcgmNuW1qxR
的id并将其传递给apidemo的uumodefied代码。我的错误在第DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
params[0]);
行
加载类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.ContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveApi.MetadataBufferResult;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveId;
import com.google.android.gms.drive.MetadataBuffer;
import com.google.android.gms.drive.query.Filters;
import com.google.android.gms.drive.query.Query;
import com.google.android.gms.drive.query.SearchableField;
public class GDriveLoadText extends BaseGDriveActivity {
private String id;
@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
}
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.MIME_TYPE, "plain/text"))
.addFilter(
Filters.eq(SearchableField.TITLE, "gdGatheredText.txt"))
.build();
Drive.DriveApi.query(getGoogleApiClient(), query).setResultCallback(
metadataCallback);
}
final private ResultCallback<MetadataBufferResult> metadataCallback = new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Problem while retrieving results");
finish();
}
MetadataBuffer metadata = result.getMetadataBuffer();
for (int i = 0; i < metadata.getCount(); i++) {
Log.d("folder id", metadata.get(i).getDriveId().toString());
Log.d("folder name", metadata.get(i).getTitle().toString());
}
if (metadata.getCount() > 0) {
String driveId = metadata.get(metadata.getCount() - 1)
.getDriveId().toString();
id =driveId.replace("DriveId:", "");
Log.d("file id", id);
}
metadata.close();
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), id)
.setResultCallback(idCallback);
}
};
final private ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
@Override
public void onResult(DriveIdResult result) {
new RetrieveDriveFileContentsAsyncTask(
GDriveLoadText.this).execute(result.getDriveId());
}
};
final private class RetrieveDriveFileContentsAsyncTask extends
ApiClientAsyncTask<DriveId, Boolean, String> {
public RetrieveDriveFileContentsAsyncTask(Context context) {
super(context);
}
@Override
protected String doInBackgroundConnected(DriveId... params) {
String contents = null;
DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
params[0]);
ContentsResult contentsResult = file.openContents(
getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null)
.await();
if (!contentsResult.getStatus().isSuccess()) {
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
contentsResult.getContents().getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
contents = builder.toString();
} catch (IOException e) {
Log.e("download text",
"IOException while reading from the stream", e);
}
file.discardContents(getGoogleApiClient(),
contentsResult.getContents()).await();
GatheredData.INSTANCE.setGatheredText(contents);
return contents;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result == null) {
showMessage("Error while reading from the file");
return;
}
showMessage("File contents: " + result);
finish();
}
}
}
基类
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
/**
* An abstract activity that handles authorization and connection to the Drive
* services.
*/
public abstract class BaseGDriveActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "BaseDriveActivity";
/**
* Extra for account name.
*/
protected static final String EXTRA_ACCOUNT_NAME = "account_name";
/**
* Request code for auto Google Play Services error resolution.
*/
protected static final int REQUEST_CODE_RESOLUTION = 1;
/**
* Next available request code.
*/
protected static final int NEXT_AVAILABLE_REQUEST_CODE = 2;
/**
* Google API client.
*/
private GoogleApiClient mGoogleApiClient;
/**
* Called when activity gets visible. A connection to Drive services need to
* be initiated as soon as the activity is visible. Registers
* {@code ConnectionCallbacks} and {@code OnConnectionFailedListener} on the
* activities itself.
*/
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
mGoogleApiClient.connect();
}
/**
* Handles resolution callbacks.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_RESOLUTION && resultCode == RESULT_OK) {
mGoogleApiClient.connect();
}
}
/**
* Called when activity gets invisible. Connection to Drive service needs to
* be disconnected as soon as an activity is invisible.
*/
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
/**
* Called when {@code mGoogleApiClient} is connected.
*/
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "GoogleApiClient connected");
}
/**
* Called when {@code mGoogleApiClient} is disconnected.
*/
@Override
public void onConnectionSuspended(int cause) {
Log.i(TAG, "GoogleApiClient connection suspended");
}
/**
* Called when {@code mGoogleApiClient} is trying to connect but failed.
* Handle {@code result.getResolution()} if there is a resolution is
* available.
*/
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
return;
}
try {
result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
} catch (SendIntentException e) {
Log.e(TAG, "Exception while starting resolution activity", e);
}
}
/**
* Shows a toast message.
*/
public void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
/**
* Getter for the {@code GoogleApiClient}.
*/
public GoogleApiClient getGoogleApiClient() {
return mGoogleApiClient;
}
}
错误
04-21 11:36:52.295: E/AndroidRuntime(6468): FATAL EXCEPTION: AsyncTask #3
04-21 11:36:52.295: E/AndroidRuntime(6468): Process: com.example.infoscrapper2, PID: 6468
04-21 11:36:52.295: E/AndroidRuntime(6468): java.lang.RuntimeException: An error occured while executing doInBackground()
04-21 11:36:52.295: E/AndroidRuntime(6468): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-21 11:36:52.295: E/AndroidRuntime(6468): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.lang.Thread.run(Thread.java:864)
04-21 11:36:52.295: E/AndroidRuntime(6468): Caused by: java.lang.IllegalArgumentException: Id must be provided.
04-21 11:36:52.295: E/AndroidRuntime(6468): at com.google.android.gms.drive.internal.l.getFile(Unknown Source)
04-21 11:36:52.295: E/AndroidRuntime(6468): at com.example.infoscrapper2.GDriveLoadText$RetrieveDriveFileContentsAsyncTask.doInBackgroundConnected(GDriveLoadText.java:84)
04-21 11:36:52.295: E/AndroidRuntime(6468): at com.example.infoscrapper2.GDriveLoadText$RetrieveDriveFileContentsAsyncTask.doInBackgroundConnected(GDriveLoadText.java:1)
04-21 11:36:52.295: E/AndroidRuntime(6468): at com.example.infoscrapper2.ApiClientAsyncTask.doInBackground(ApiClientAsyncTask.java:63)
04-21 11:36:52.295: E/AndroidRuntime(6468): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-21 11:36:52.295: E/AndroidRuntime(6468): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-21 11:36:52.295: E/AndroidRuntime(6468): ... 4 more