我在邮递员上传递了一个更新用户请求,并获得了成功的响应(见图)。现在,当我尝试使用Retrofit 2在我的应用程序中执行相同的操作时,出现了错误com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
。有趣的是,我较早地遇到了这个错误,并且我知道如果我的模型不符合响应,通常会发生这种情况。但是这一次我想我已经检查了所有复选框,但是仍然遇到错误。如果有人能弄清楚我要去哪里错了...
我在邮递员中的回复
我的Pojo:
package com.example.evidya.Retrofit.Model.EditModel;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class EditResponse {
@SerializedName("result")
@Expose
private String result;
@SerializedName("response_code")
@Expose
private Integer responseCode;
@SerializedName("msg")
@Expose
private String msg;
@SerializedName("data")
@Expose
private List<Object> data = null;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public Integer getResponseCode() {
return responseCode;
}
public void setResponseCode(Integer responseCode) {
this.responseCode = responseCode;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List<Object> getData() {
return data;
}
public void setData(List<Object> data) {
this.data = data;
}
}
我的改造客户:
package com.example.evidya.Retrofit;
import com.example.evidya.Common.Common;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
public static Retrofit retrofit = null;
public static Retrofit getRetrofit(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder() //adding custom interceptor
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS);
httpClient.addInterceptor(loggingInterceptor); //add logging interceptor as the last interceptor,
// because this shall also show other interceptors
Gson gson = new GsonBuilder()
.setLenient()
.create();
if(retrofit == null){
retrofit= new Retrofit.Builder()
.baseUrl(Common.baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();
}
return retrofit;
}
}
改造界面:
@FormUrlEncoded
@PUT("user/update_user")
Call<EditResponse> updateUser(
@HeaderMap Map<String, String> headers,
@Field("user_id") String id,
@Field("email") String email,
@Field("name") String name,
@Field("phone") String phone,
@Field("access_token") String accessToken
);
我的代码:
private void saveRequest() {
Log.d(TAG, "saveRequest: "+loginResponseData.getId());
progressDialog.setMessage("Updating...");
progressDialog.show();
String name, email, mobile;
name = mName.getText().toString();
email = mEmail.getText().toString();
mobile = mPhoneNumber.getText().toString();
Call<EditResponse> editResponseCall = evidya.updateUser(Common.getHeaders(), loginResponseData.getId(), email, name, mobile, loginResponseData.getAccessToken());
editResponseCall.enqueue(new Callback<EditResponse>() {
private int retryCount = 0;
@Override
public void onResponse(Call<EditResponse> call, Response<EditResponse> response) {
progressDialog.dismiss();
if(!response.isSuccessful()){
Toast.makeText(Edit_AccountActivity.this, ""+response.message(), Toast.LENGTH_SHORT).show();
return;
}
EditResponse loginResponse = null;
try {
loginResponse = response.body();
} catch (Exception e) {
Log.d(TAG, "onResponse: error parsing"+e.toString());
}
if(loginResponse != null){
if(loginResponse.getResult().toLowerCase().equals("success")){
Toast.makeText(Edit_AccountActivity.this, "Your details successfully updated", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(Edit_AccountActivity.this, ""+loginResponse.getMsg(), Toast.LENGTH_SHORT).show();
}
} else{
Toast.makeText(Edit_AccountActivity.this, "Invalid Response from server", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<EditResponse> call, Throwable t) {
Log.d(TAG, "onFailure: "+t.toString());
Toast.makeText(Edit_AccountActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}
答案 0 :(得分:0)
通常在您收到服务器发出的预期响应以外的内容时发生。
要了解问题所在,请尝试在Postman中模仿您的请求,然后查看您从服务器收到的内容。
此外,您可以打开OkHttp's Logging Interceptor来查看服务器在Logcat中返回的内容。
答案 1 :(得分:0)
添加@Headers({“ Accept:application / json”}) 在翻新界面中为我工作
@Headers({"Accept: application/json"})
@POST("user/classes")
Call<playlist> addToPlaylist(@Body PlaylistParm parm);