Android Retrofit 2.2.0预计BEGIN_OBJECT但在STRING第1列第1列路径$

时间:2017-03-24 12:39:00

标签: android retrofit2

我使用Retrofit库从服务器获取数据,但它给了我像

这样的例外
@FormUrlEncoded
    @POST(ApiClient.LIST_COMMENTS)
    Call<CommentResponse> listComments (@Field(SPOOF_ID) String spoof_id);

APIService apiService = ApiClient.getClient ().create (APIService.class);
        Call<CommentResponse> commentResp = apiService.listComments ("1");
        commentResp.enqueue (new Callback<CommentResponse> (){
            @Override
            public void onResponse (Call<CommentResponse> call, Response<CommentResponse> response){
                CustomDialog.dismissProgressDialog ();
                CommentResponse commentResponse = response.body ();

            }

            @Override
            public void onFailure (Call<CommentResponse> call, Throwable t){
                Log.d (LOGTAG, "Failure " + t.getMessage ());
                CustomDialog.dismissProgressDialog ();
            }
        });

首先,当我调用webservice时,它会给出如下错误

失败使用JsonReader.setLenient(true)接受第1行第1行路径中的格式错误的JSON $

使用以下代码解决了以上错误

 public static Retrofit getClient (){
        Gson gson = new GsonBuilder ()
                .setLenient()
                .create();

        //OkHttpClient okHttpClient = new OkHttpClient ().newBuilder ().connectTimeout (60, TimeUnit.SECONDS).readTimeout (60, TimeUnit.SECONDS).writeTimeout (60, TimeUnit.SECONDS).build ();
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient ().newBuilder ().connectTimeout (60, TimeUnit.SECONDS).addInterceptor (logging).build ();
        if (retrofit == null){

            retrofit = new Retrofit.Builder ().baseUrl (WEBURL).client (okHttpClient).addCallAdapterFactory (RxJavaCallAdapterFactory.create ()).addConverterFactory (GsonConverterFactory.create (gson)).build ();
        }
        return retrofit;
    }

现在我遇到错误

失败java.lang.IllegalStateException:预期BEGIN_OBJECT但是第1行第1行STRING

JSON:

{
  "success": true,
  "error": false,
  "msg": "comments found",
  "list_commets": [
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-02 12:15:05",
      "comment": "Nice Spoof",
      "id": "9",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-02 12:14:47",
      "comment": "Nice Spoof sir",
      "id": "8",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-01 03:29:18",
      "comment": "Very nice sppof ",
      "id": "7",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-01 03:29:02",
      "comment": "Very nice sppof ",
      "id": "6",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-01 03:28:43",
      "comment": "Very nice sppof ",
      "id": "5",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "4",
      "points": "1",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/4\/414881955115.png",
      "added_date": "2017-03-01 03:28:18",
      "comment": "Very nice sppof ",
      "id": "4",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "1",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "1",
      "points": "3",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/1\/14881916841.png",
      "added_date": "2017-02-27 02:10:56",
      "comment": "Nice Spoof",
      "id": "1",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "3",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    },
    {
      "user_id": "3",
      "points": "2",
      "user_image_url": "http:\/\/projects.resilienceinfosolutions.com\/spoof\/public\/user_images\/3\/414881955115.png",
      "added_date": "2017-02-27 02:10:56",
      "comment": "Nice Spoof",
      "id": "2",
      "user_functionality": {
        "doodles_status": "off",
        "memes_status": "off",
        "points_exchange_status": "off",
        "total_points": "2",
        "crown_status": "default",
        "crown_color_rgb": "rgb(255,255,255)",
        "crown_color_hex": "#FFFFFF"
      }
    }
  ],
  "list_commets_count": 8
}

上课:

public class CommentResponse{

    @SerializedName("list_commets")
    @Expose
    public List<ListCommet> listCommets = new ArrayList<> ();
    @SerializedName("list_commets_count")
    @Expose
    public int listCommetsCount;
    @SerializedName("success")
    @Expose
    private boolean success;
    @SerializedName("error")
    @Expose
    private boolean error;
    @SerializedName("msg")
    @Expose
    private String msg;

    public boolean isSuccess (){
        return success;
    }

    public void setSuccess (boolean success){
        this.success = success;
    }

    public boolean isError (){
        return error;
    }

    public void setError (boolean error){
        this.error = error;
    }

    public String getMsg (){
        return msg;
    }

    public void setMsg (String msg){
        this.msg = msg;
    }

    public List<ListCommet> getListCommets (){
        return listCommets;
    }

    public void setListCommets (List<ListCommet> listCommets){
        this.listCommets = listCommets;
    }

    public int getListCommetsCount (){
        return listCommetsCount;
    }

    public void setListCommetsCount (int listCommetsCount){
        this.listCommetsCount = listCommetsCount;
    }

    public class ListCommet{

        @SerializedName("user_id")
        @Expose
        private String userId;
        @SerializedName("points")
        @Expose
        private String points;
        @SerializedName("user_image_url")
        @Expose
        private String userImageUrl;
        @SerializedName("added_date")
        @Expose
        private String addedDate;
        @SerializedName("comment")
        @Expose
        private String comment;
        @SerializedName("id")
        @Expose
        private String id;
        @SerializedName("user_functionality")
        @Expose
        private UserFunctionality userFunctionality;

        public String getUserId (){
            return userId;
        }

        public void setUserId (String userId){
            this.userId = userId;
        }

        public String getPoints (){
            return points;
        }

        public void setPoints (String points){
            this.points = points;
        }

        public String getUserImageUrl (){
            return userImageUrl;
        }

        public void setUserImageUrl (String userImageUrl){
            this.userImageUrl = userImageUrl;
        }

        public String getAddedDate (){
            return addedDate;
        }

        public void setAddedDate (String addedDate){
            this.addedDate = addedDate;
        }

        public String getComment (){
            return comment;
        }

        public void setComment (String comment){
            this.comment = comment;
        }

        public String getId (){
            return id;
        }

        public void setId (String id){
            this.id = id;
        }

        public UserFunctionality getUserFunctionality (){
            return userFunctionality;
        }

        public void setUserFunctionality (UserFunctionality userFunctionality){
            this.userFunctionality = userFunctionality;
        }

        public class UserFunctionality{

            @SerializedName("doodles_status")
            @Expose
            private String doodlesStatus;
            @SerializedName("memes_status")
            @Expose
            private String memesStatus;
            @SerializedName("points_exchange_status")
            @Expose
            private String pointsExchangeStatus;
            @SerializedName("total_points")
            @Expose
            private String totalPoints;
            @SerializedName("crown_status")
            @Expose
            private String crownStatus;
            @SerializedName("crown_color_rgb")
            @Expose
            private String crownColorRgb;
            @SerializedName("crown_color_hex")
            @Expose
            private String crownColorHex;

            public String getDoodlesStatus (){
                return doodlesStatus;
            }

            public void setDoodlesStatus (String doodlesStatus){
                this.doodlesStatus = doodlesStatus;
            }

            public String getMemesStatus (){
                return memesStatus;
            }

            public void setMemesStatus (String memesStatus){
                this.memesStatus = memesStatus;
            }

            public String getPointsExchangeStatus (){
                return pointsExchangeStatus;
            }

            public void setPointsExchangeStatus (String pointsExchangeStatus){
                this.pointsExchangeStatus = pointsExchangeStatus;
            }

            public String getTotalPoints (){
                return totalPoints;
            }

            public void setTotalPoints (String totalPoints){
                this.totalPoints = totalPoints;
            }

            public String getCrownStatus (){
                return crownStatus;
            }

            public void setCrownStatus (String crownStatus){
                this.crownStatus = crownStatus;
            }

            public String getCrownColorRgb (){
                return crownColorRgb;
            }

            public void setCrownColorRgb (String crownColorRgb){
                this.crownColorRgb = crownColorRgb;
            }

            public String getCrownColorHex (){
                return crownColorHex;
            }

            public void setCrownColorHex (String crownColorHex){
                this.crownColorHex = crownColorHex;
            }
        }
    }
}

0 个答案:

没有答案