我试图获取google驱动器中的文件夹列表,
我已经成功注册了我的应用程序,代码一直运行到onConnected()
方法,
这是我使用的代码:
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.");
}
};
}
如果我只执行onConnected
方法etxt2.setText("connection success !!!");
中的第一行,我会收到消息:&#34;连接成功!!!&#34;,表示链接成功,但是当我尝试执行其余操作以获取文件夹的名称时,我收到此错误:
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)
任何帮助将不胜感激,提前谢谢。
答案 0 :(得分:3)
如错误消息所示&#34;未请求适当的Api&#34;。您没有在GoogleApiClient中请求正确的API。您需要包含Drive.API
答案 1 :(得分:0)
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
您应该将此更改为:addApi(Plus.API)
到此addApi(Drive.API)