这是一个非常奇怪的现象。 我的应用使用Android Google Drive SDK为每个用户存储两个数据文件。 这些文件位于app文件夹中。要获得与Google服务的连接,请使用:
PlusOptions plusOptions = new Plus.PlusOptions.Builder()
.addActivityTypes("http://schemas.google.com/AddActivity").build();
mGoogleApi = new GoogleApiClient.Builder(this).addApi(Plus.API, plusOptions).addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE).addApi(Drive.API).addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(this).addOnConnectionFailedListener(this)
.build();
我可以创建这些文件并编写它们。 但有时,两个文件消失,其内容丢失。 logcat流包含以下消息:
05-16 10:15:18.316: W/GLSUser(829): [apk][apk] Permission for debut.kholle to access oauth2:https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me will be managed remotely.
05-16 10:15:18.416: I/qtaguid(14877): Failed write_ctrl(u 56) res=-1 errno=22
05-16 10:15:18.416: I/qtaguid(14877): Untagging socket 56 failed errno=-22
05-16 10:15:18.416: W/NetworkManagementSocketTagger(14877): untagSocket(56) failed with errno -22
05-16 10:15:18.416: I/imp(14877): I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
05-16 10:15:18.416: I/imp(14877): Retrying request
05-16 10:15:18.436: W/GLSUser(829): [apk][apk] Permission for debut.kholle to access oauth2:https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me will be managed remotely.
05-16 10:15:18.516: W/GLSUser(829): GoogleAccountDataService.getToken()
05-16 10:15:18.546: I/qtaguid(21108): Failed write_ctrl(u 85) res=-1 errno=22
05-16 10:15:18.546: I/qtaguid(21108): Untagging socket 85 failed errno=-22
05-16 10:15:18.546: W/NetworkManagementSocketTagger(21108): untagSocket(85) failed with errno -22
05-16 10:15:18.546: I/imp(21108): I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
05-16 10:15:18.546: I/imp(21108): Retrying request
05-16 10:15:19.576: E/SyncSchedulerImpl(14877): Ignoring sync request: rate limited (on connection limit)
修改
这是我的代码,用于读取包含应用程序数据的文件。此代码包含在活动onCreate
过程中:
monGoogleApiLire = new GoogleApiClient.Builder(this).addApi(Drive.API).addScope(Drive.SCOPE_FILE)
.addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Filter titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_PLANNING);
Query query = new Query.Builder().addFilter(titreFilter).build();
Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
.setResultCallback(new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
// showMessage("Error while trying to create the file");
return;
}
MetadataBuffer meta = result.getMetadataBuffer();
if (meta.getCount() != 0) {
int nb = meta.getCount();
DriveId idc = null;
for (int i = 0; i < nb; i++) {
idc = meta.get(i).getDriveId();
}
DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
// display an error saying file can't be opened
return;
}
// Contents object contains pointers
// to the actual byte stream
Contents contents = result.getContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String contentsAsString = builder.toString();
reader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contents.close();
// fichier.commitAndCloseContents(gapi, contents);
}
});
} else {
monPlanning = new ArrayList<DonneesPlanning>();
afficherPlanning();
if (monPlanning != null && mesCours != null && mesLieux != null) {
ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
monGoogleApiLire.disconnect();
}
// monGoogleApiLire.disconnect();
}
meta.close();
}
});
titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_COURS);
query = new Query.Builder().addFilter(titreFilter).build();
Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
.setResultCallback(new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
// showMessage("Error while trying to create the file");
return;
}
MetadataBuffer meta = result.getMetadataBuffer();
if (meta.getCount() != 0) {
int nb = meta.getCount();
DriveId idc = null;
for (int i = 0; i < nb; i++) {
idc = meta.get(i).getDriveId();
}
DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
// display an error saying file can't be opened
return;
}
// Contents object contains pointers
// to the actual byte stream
Contents contents = result.getContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String contentsAsString = builder.toString();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contents.close();
// fichier.commitAndCloseContents(gapi, contents);
}
});
} else {
mesCours = new ArrayList<DonneesCours>();
if (monPlanning != null && mesCours != null && mesLieux != null) {
ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
monGoogleApiLire.disconnect();
}
}
meta.close();
}
});
titreFilter = Filters.eq(SearchableField.TITLE, NOM_FICHIER_LIEUX);
query = new Query.Builder().addFilter(titreFilter).build();
Drive.DriveApi.getAppFolder(monGoogleApiLire).queryChildren(monGoogleApiLire, query)
.setResultCallback(new ResultCallback<MetadataBufferResult>() {
@Override
public void onResult(MetadataBufferResult result) {
if (!result.getStatus().isSuccess()) {
// showMessage("Error while trying to create the file");
return;
}
MetadataBuffer meta = result.getMetadataBuffer();
if (meta.getCount() != 0) {
int nb = meta.getCount();
DriveId idc = null;
for (int i = 0; i < nb; i++) {
idc = meta.get(i).getDriveId();
}
DriveFile fichier = Drive.DriveApi.getFile(monGoogleApiLire, idc);
fichier.openContents(monGoogleApiLire, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
// display an error saying file can't be opened
return;
}
// Contents object contains pointers
// to the actual byte stream
Contents contents = result.getContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String contentsAsString = builder.toString();
reader.close();
if (monPlanning != null && mesCours != null
&& mesLieux != null) {
ActiviteKholle.this
.setProgressBarIndeterminateVisibility(false);
monGoogleApiLire.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contents.close();
// fichier.commitAndCloseContents(gapi, contents);
}
});
} else {
mesLieux = new ArrayList<DonneesLieu>();
if (monPlanning != null && mesCours != null && mesLieux != null) {
ActiviteKholle.this.setProgressBarIndeterminateVisibility(false);
monGoogleApiLire.disconnect();
}
}
meta.close();
}
});
}
@Override
public void onConnectionSuspended(int cause) {
// TODO Auto-generated method stub
}
}).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
}).build();
monGoogleApiLire.connect();
对此有解释吗?
P.S。经过大量的试验和测试,我现在确定该领域&#34; monGoogleApi&#34;当我测试文件存在时连接。当我使用&#34; queryChildren&#34;在代码行上按名称和mime类型搜索文件:
if (meta.getCount() != 0) {
...
}
在15分钟内,meta.getCount等于1,15分钟后,meta.getCount等于0。
答案 0 :(得分:1)
您是否检查过“monGoogleApi”的连接状态?
此连接可能有会话超时。