嗨,这是我试图获取文本文件的方法。我尝试了同样的方法
- 新线程
同样,当我创建新线程时,它不会仅出现在 getfiles(Drive services) 方法中。请给我解决方案我没有得到这个。
使用 main 运行错误可能导致死锁
公共类 MainActivity 扩展 Activity {
static final int REQUEST_ACCOUNT_PICKER = 1;
private static Uri fileUri;
private static Drive service;
private GoogleAccountCredential credential;
Button b;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.b1);
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
Toast.makeText(getApplicationContext(), "case1", Toast.LENGTH_LONG).show();
}
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
{
if(requestCode==REQUEST_ACCOUNT_PICKER)
{
if (resultCode == RESULT_OK && data != null && data.getExtras() != null)
{
Toast.makeText(getApplicationContext(), "case2", Toast.LENGTH_LONG).show();
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null)
{
credential.setSelectedAccountName(accountName);
service = getDriveService(credential);
Toast.makeText(getApplicationContext(), "Got the E mail id", Toast.LENGTH_LONG).show();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "case4", Toast.LENGTH_LONG).show();
getfiles(service);
}
});
}
}
}
}
public void getfiles(final Drive service)
{
Toast.makeText(getApplicationContext(), "case5", Toast.LENGTH_LONG).show();
String TEXT_PLAIN = "TEXT/PLAIN";
try
{
Files.List request = service.files().list().setQ("mimeType = '" + TEXT_PLAIN +"'");
Toast.makeText(getApplicationContext(), "case", Toast.LENGTH_LONG).show();
Map<String, File> textFiles = new HashMap<String, File>();
do {
Toast.makeText(getApplicationContext(), "case6", Toast.LENGTH_LONG).show();
FileList files = request.execute(); //this is error line
Toast.makeText(getApplicationContext(), "case7", Toast.LENGTH_LONG).show();
for (File file : files.getItems())
{
textFiles.put(file.getId(), file);
}
request.setPageToken(files.getNextPageToken());
} while (request.getPageToken() != null && request.getPageToken().length() > 0);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
private Drive getDriveService(GoogleAccountCredential credential) {
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}