当前位置: 首页 > 知识库问答 >
问题:

改装2.0:应为BEGIN_OBJECT,但在第1行第1列路径$[DUPLICAT]处为字符串

潘秦斩
2023-03-14

我在postman上传递了一个update user请求,并得到了一个成功的响应(参见图片),现在,当我尝试在应用程序中使用Reverfit 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();
        }


    });
}

共有1个答案

莘羽
2023-03-14

当您收到来自服务器的响应以外的东西时,通常会发生这种情况。
要了解错误所在,请尝试在Postman中模仿您的请求,看看您从服务器收到了什么。
此外,您可以打开OKHTTP的日志拦截器,看看服务器在您的logcat中确切地返回了什么。

 类似资料: