7
アンドロイド優先ジョブキューを使用していますが、私の残りのAPIに同期呼び出しを行うためにretrofitを使用していますが、401のようなエラーを処理する方法がわかりません。エラーを示すバックjson。シンプルな非同期呼び出しを行うが、私はジョブマネージャーのための私のアプリを適応している。以下はIO例外のシンプルなtry catchですが、401の422などは?これを行う方法?4xxエラーの同期呼び出しエラー処理を再調整する
try {
PostService postService = ServiceGenerator.createService(PostService.class);
final Call<Post> call = postService.addPost(post);
Post newPost = call.execute().body();
// omitted code here
} catch (IOException e) {
// handle error
}
EDIT
改造レスポンスオブジェクトを使用すると、改造レスポンスオブジェクトは
Response<Post> response = call.execute();
if (response.isSuccessful()) {
// request successful (status code 200, 201)
Post result = response.body();
// publish the post added event
EventBus.getDefault().post(new PostAddedEvent(result));
} else {
// request not successful (like 400,401,403 etc and 5xx)
renderApiError(response);
}
はあなたを助けるために、レスポンスオブジェクトは、私は必要なものだった歓声:)うれしい – CaptRisky
を使用して、ありがとうございます。 –
同じことをすると、call.executeが空の値を持つjsonを返すので、 'java.lang.NumberFormatException:無効なdouble:" "というエラーが発生します。私はcall.execute()の本体を取得し、それ以外の処理を行う前にチェックしたいと思います。私が " newPostResponse = call.execute();"のような文を書くと、私は体が正しくないので、エラーが発生します –