Retrofit2 Handle condition when status code 200 but json structure different than datamodel class

后端 未结 5 444
-上瘾入骨i
-上瘾入骨i 2021-02-01 22:09

I\'m using Retrofit2 and RxJava2CallAdapterFactory.

The API I consume returns status code always as 200 and for success and response JSON string the JSON st

5条回答
  •  醉梦人生
    2021-02-01 22:55

    It seems out that Retrofit's use of Gson by default makes it easy to add a custom deserialize to handle the portion of the JSON document that was the problem.

    Sample code

    @FormUrlEncoded
        @POST(GlobalVariables.LOGIN_URL)
        void Login(@Field("email") String key, @Field("password") String value, Callback callback);
    
    getService().Login(email, password, new MyCallback(context, true, null)
    {
        @Override
        public void failure(RetrofitError arg0)
         {
            // TODO Auto-generated method stub
            UtilitySingleton.dismissDialog((BaseActivity) context);
            System.out.println(arg0.getResponse());
          }
    
        @Override
        public void success(Response arg0, Response arg1)
        {
             String result = null;
             StringBuilder sb = null;
             InputStream is = null;
             try
             {
                    is = arg1.getBody().in();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                        result = sb.toString();
                        System.out.println("Result :: " + result);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    

提交回复
热议问题