![](/img/trans.png)
[英]Retrofit Error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
[英]Retrofit and gson Error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
我正在使用改造2.4.0和gson 2.8.5,我得到了這個異常:
java.lang.IllegalStateException:預期為BEGIN_OBJECT,但位於第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();
}
});
gson轉換器會錯誤,除了提供字符串的類對象外。
Gson converter convert the json response to java object
and java object to the json respectively
根據響應創建適當的pojo,請參考以下網站進行pojo的創建: http : //www.jsonschema2pojo.org/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.