Retrofit和gson错误:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1列路径$

时间:2018-12-22 10:33:03

标签: java android api gson retrofit2

我正在使用翻新版2.4.0和gson 2.8.5,但出现了这个异常:

  

java.lang.IllegalStateException:预期为BEGIN_OBJECT,但为STRING   在第1行第1列的路径$

我搜索了很多类似的问题,没有人给我解决方案。任何人都可以帮助我解决此错误。

gradle:

implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

服务器响应为:

{
    "status": "success",
    "result": 1,
    "message": "Profile successfully updated"
}

ResponseModel:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ResponseModel {

@SerializedName("status")
@Expose
private String status;
@SerializedName("result")
@Expose
private Integer result;
@SerializedName("message")
@Expose
private String message;

 public String getStatus() {
    return status;
}

 public Integer getResult() {
    return result;
 }

public void setMessage(String message) {
    this.message = message;
 }
}

ApiInterface:

public interface ApiInterface {

   public static final String BASE_URL ='..';

    @Headers({"Content-Type: multipart/form-data",
        "Accept: application/json"})
    @Multipart
    @POST("/profile/update")
    Call<ResponseModel> uploadImage(@Header("X-Header") String defaultHeader,
                                @Header("Authorization") String authHeader,
                                @Part MultipartBody.Part image,
                                @Part("name") RequestBody name,
                                @Part("gender") RequestBody gender);


   public class ApiClient {
        public static ApiInterface apiInterface;
        public static ApiInterface getApiInterface() {
            if (apiInterface == null) {

               Gson gson = new GsonBuilder()
                        .setLenient()
                        .create();

                OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(okHttpClient)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();

                apiInterface = retrofit.create(ApiInterface.class);
                return apiInterface;
            } else {
                return apiInterface;
            }
        }
 }

在MainActivity.java

 File file = new File(ImagePath);
            RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
            MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
            RequestBody ReqBodyName = RequestBody.create(MediaType.parse("text/plain"), nameValue);
            RequestBody ReqBodyGender = RequestBody.create(MediaType.parse("text/plain"), genderValue);

ApiInterface.ApiClient.getApiInterface().uploadImage(DEFAULT_HEADER,AUTH_HEADER,fileToUpload,ReqBodyName,ReqBodyGender).enqueue(new Callback<ResponseModel>() {
                @Override
                public void onResponse(@NonNull Call<ResponseModel> call, @NonNull Response<ResponseModel> response) {
                    Log.e("TAG", "resp"+new Gson().toJson(response.body()));
                    Log.d("Log",response.raw().toString());
                    Log.d("Log Status",response.body().getStatus());
                    Log.d("Log Message",response.body().getMessage());


                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this, "response"+response.body().getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFailure(@NonNull Call<ResponseModel> call, @NonNull Throwable t) {
                    progressBar.setVisibility(View.GONE);
                    Log.d("Error====",t.getMessage());
                    Toast.makeText(MainActivity.this, "Error :"+t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

1 个答案:

答案 0 :(得分:0)

gson转换器会出错,除了它给出字符串的类对象之外。

Gson converter convert the json response to java object 
and java object to the json respectively 

根据响应创建适当的pojo,请参考此网站制作pojo:http://www.jsonschema2pojo.org/