Response in 202 STATUS in Retrofit2.0

我的梦境 提交于 2019-12-25 18:34:30

问题


i want to convert the Response in to JSON and show it to the User .I was able to display 200 response but in 202,i am failed.

{



            Log.d(TAG, "RESPONSE CODE" + response.code());
            if (response.code() == 200) {
                Gson gson = new Gson();
                SuccessResponse signupResponse = response.body();
                String sSignupResponse = gson.toJson(signUpResponse, SuccessResponse.class);
                try {

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } else if (response.code() == 202) {
                Log.d(TAG,"RESPONSE CODE IS "+"202 RIGHT");
                Gson gson = new Gson();
                SuccessResponse signupResponse = response.body();
                Log.d(TAG,"WHAT IS sSignupResponse"+signupResponse.toString());
                String sSignupResponse = gson.toJson(signUpResponse.toString());
                Log.d(TAG,"WHAT IS sSignupResponse"+sSignupResponse.toString());
                try {
                    JSONObject jsonObject=new JSONObject(sSignupResponse);
                    Log.d(TAG,"WHAT IS jsonObject"+jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            } else {
                //request not successful (like 400,401,403 etc)
                //Handle errors
                Converter<ResponseBody, ErrorResponse> converter = ApiClient.getRetrofit().responseBodyConverter(ErrorResponse.class, new Annotation[0]);
                try {
                    ErrorResponse errors = converter.convert(response.errorBody());
                    dialogUtil.showOkDialog(errors.getMessage().toString());
                } catch (Exception e) {
                    dialogUtil.showOkDialog(e.getMessage().toString());
                }
            }

        }

should i do the same for 202 too? i created another POJO like ErrorResponse with message and code ,replaced the SuccessRespone with ErrorResponse


回答1:


try this:

String sSignupResponse = gson.toJson(signUpResponse);

instead of

String sSignupResponse = gson.toJson(signUpResponse, SuccessResponse.class);



回答2:


200 (OK), 201(Created - when you insert something in server database) and 204(No content - server has processed your request and it don't return nothing instead) are corect. Check here for all response codes in HTTP/1.1




回答3:


You can take a look at REST response code HERE

If response code is match with format 2xx, that means success. So, in you case you should check response.isSuccessful() to check if the response is success or not instead of check response code by manual




回答4:


I got an answer,Just do like this .You know upfront the 202 Response.So make a POJO class Response202.Now let SuccessResponse Class inherit the properties of Response Class.You are done now

SuccessResponse response202 = response.body();
                        Gson gson202 = new Gson();
                        String json202Str = gson202.toJson(response202, SuccessResponse.class);


来源:https://stackoverflow.com/questions/43673957/response-in-202-status-in-retrofit2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!