【问题标题】:Retrofit and gson Error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $改造和 gson 错误:java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 1 列路径 $
【发布时间】:2018-12-22 10:33:02
【问题描述】:

我正在使用改造 2.4.0 和 gson 2.8.5,我遇到了这个异常:

java.lang.IllegalStateException: 应为 BEGIN_OBJECT 但为 STRING 在第 1 行第 1 列路径 $

我已经搜索了很多类似的问题,没有人给我解决方案。请任何人帮我解决这个错误。

分级:

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"
}

响应模型:

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;
 }
}

API接口:

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();
                }
            });

【问题讨论】:

  • 发布 onResponse 和 onfaliure
  • 好的,等我更新问题
  • @AL Tegani,请检查更新的问题
  • 显示哪个响应日志打印
  • 没有我从 android 应用程序收到错误

标签: java android api gson retrofit2


【解决方案1】:

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

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

根据响应创建正确的 pojo 参考此站点进行 pojo :http://www.jsonschema2pojo.org/

【讨论】:

    猜你喜欢
    • 2021-01-06
    • 2018-01-10
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2020-11-09
    相关资源
    最近更新 更多