我有一個非Android Java應用程序一個接一個上傳數千個文件。捕獲GoogleJsonResponseException但e.getDetails()返回null
對於每個插入,我執行指數退避像這樣:
Random randomGenerator = new Random();
for(int i = 0; i < 5; i++) {
try {
return service.files().insert(body, mediaContent).execute();
} catch(GoogleJsonResponseException e) {
if (e.getDetails().getCode() == 500
|| e.getDetails().getCode() == 501
|| e.getDetails().getCode() == 502
|| e.getDetails().getCode() == 503
|| e.getDetails().getCode() == 504
|| e.getDetails().getCode() == 403) {
Thread.sleep((1 << i) * 1000
+ randomGenerator.nextInt(1001));
} else {
// error besides 5xx
throw e;
}
} catch (HttpResponseException e) {
if (e.getStatusCode() == 500 || e.getStatusCode() == 501
|| e.getStatusCode() == 502 || e.getStatusCode() == 503
|| e.getStatusCode() == 504
|| e.getStatusCode() == 403) {
Thread.sleep((1 << i) * 1000
+ randomGenerator.nextInt(1001));
} else {
// error besides 5xx
throw e;
}
} catch (SocketTimeoutException e) {
Thread.sleep((1 << i) * 1000 + randomGenerator.nextInt(1001));
}
這裏的問題:
已經有多次在我的應用程序接球GoogleJsonResponseException,但ē .getDetails()返回null
因此,當檢查錯誤代碼時,我得到空指針異常。
Returns the Google JSON error details or null for none (for example if response is not JSON).
所以基本上,我趕上一個GoogleJsonResponseException
但沒有JSON。
爲什麼在世界上我可以捕捉到GoogleJsonResponseException
,但實際上並沒有任何Json與之關聯?爲什麼沒有它捕獲HttpResponseException
?
我希望我的應用程序能夠在返回時捕獲/使用Json,但我不知道如何確保GoogleJsonResponseException
甚至不能保證返回Json。
有什麼建議嗎?
我應該只是用HttpResponseException
來可靠地檢查statusCode,然後看看getMessage()
看看是否有JSON?
或者我應該只使用GoogleJsonResponseException
但調用它的父HttpResponseException
方法getStatusCode()
看到狀態代碼,只有getDetails()
,如果我真的想檢查/潛入任何可能的Json?
該指數倒轉是使用驅動API V2客戶端LIB(1.19.0),並在https://developers.google.com/drive/web是基於谷歌關閉當前的Java代碼示例/處理錯誤 我認爲谷歌需要更新此頁面... – mmcrae 2014-09-02 14:25:57
你真的需要捕捉一個403來處理流量控制,並且你需要跟蹤http來查看網絡上發生了什麼。我建議你將問題發佈到https://groups.google.com/forum/#!forum/google-http-java-client – pinoyyid 2015-01-29 04:49:24
,正是我所檢查的狀態代碼是無關緊要的 - 問題集中在「 null'由'GoogleJsonResponseException.getDetails()'返回。感謝谷歌論壇鏈接...我會嘗試。 – mmcrae 2015-01-29 15:23:06