简体   繁体   中英

Retrofit and gson Error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

Im using retrofit 2.4.0 and gson 2.8.5 , i got this exception :

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I have searched so many similar question , no one gives me solution .any one please help me to solve this error.

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'

Server response is :

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

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

gson converter error it except the class object it gives the string.

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

Create the proper pojo according to the response refer this site for making pojo : http://www.jsonschema2pojo.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM