gpt4 book ai didi

java - 改造和 gson 错误 : java. lang.IllegalStateException:应为 BEGIN_OBJECT 但在第 1 行第 1 列路径 $

转载 作者:行者123 更新时间:2023-11-30 05:10:02 24 4
gpt4 key购买 nike

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

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

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

渐变:

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接口(interface):

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/

关于java - 改造和 gson 错误 : java. lang.IllegalStateException:应为 BEGIN_OBJECT 但在第 1 行第 1 列路径 $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com