Android 改造gson JsonSyntaxException解析问题begin对象

Android 改造gson JsonSyntaxException解析问题begin对象,android,json,Android,Json,错误: POJO: -创建显示json对象数据的Pojo类用户详细信息 应为BEGIN_对象,但在第2行第1列路径处为字符串$ LoginResponse { @SerializedName("status") private Integer status; @SerializedName("message") private String message; @SerializedName("data") private User user; }

错误:

POJO:

-创建显示json对象数据的Pojo类用户详细信息 应为BEGIN_对象,但在第2行第1列路径处为字符串$

LoginResponse {
    @SerializedName("status")
    private Integer status;
    @SerializedName("message")
    private String message;
    @SerializedName("data")
    private User user;
}
我的Json输出像

 class User {
    @SerializedName("id")
    @Expose
    private String id;
}
为改型Api解析创建接口

@姿势/登录 呼叫userLogIn@Body用户登录

下面的主活动代码应为BEGIN_对象,但在第2行第1列路径处为字符串$

LoginResponse {
    @SerializedName("status")
    private Integer status;
    @SerializedName("message")
    private String message;
    @SerializedName("data")
    private User user;
}
APILogin服务=ApiClient.getClient.createAPILogin.class; 用户登录=新用户; login.setEmail; login.setPasswordpassword

{
    "status": 1,
    "message": "You are successfully logged in.",
    "data": {
        "id": "10",
        "email": "abcdef@gmail.com",
        "password": "3f009d72559f51e7e454b16e5d0687a1",
        "mobile": "abc@gmail.com",
        "verified_saller": "",
        "first_name": "abc@gmail.com",
        "middle_name": "abc@gmail.com",
        "last_name": "abc@gmail.com",
        "image": "",
        "default_size": "0",
        "wallet": "",
        "status": "active",
        "otp": "",
        "updated": "2017-10-28 12:22:21",
        "created": "2017-10-28 11:41:14"
    }
}
JSON字符串从错误消息中可以看出,它不是要解析到Ad类中的正确结构

从您的错误中,我可以看出它不是解析到类中的正确结构

Gson希望您的JSON字符串以一个对象大括号开头。e、 g.{不与

从后端生成具有正确结构的JSON响应


将HttpResponseModel类创建为fllow

HttpResponseModel.java

    Call<User> userCall = service.userLogIn(login);
    userCall.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            User user = response.body();
            mRelativeLayout.setVisibility(View.GONE);
            mcardView.setVisibility(View.VISIBLE);
            //onSignupSuccess();
            Log.d("onResponse", "" + response.body().getMessage());
            if (response.body().getStatus() == 1) {
                Toast.makeText(LoginActivity.this, "" + response.body().getMessage(), Toast.LENGTH_SHORT).show();



                Intent i = new Intent(LoginActivity.this, HomeActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);
                finish();
            } else {
                Toast.makeText(LoginActivity.this, "" + response.body().getMessage(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            mRelativeLayout.setVisibility(View.GONE);
            mcardView.setVisibility(View.VISIBLE);
            call.cancel();
            Toast.makeText(LoginActivity.this, "Please check your network connection and internet permission" + t.getMessage(), Toast.LENGTH_LONG).show();
            Log.d("onFailure", t.toString());
        }
    });
您的改装响应如下

public class HttpResponseModel {

    @SerializedName("status")
    @Expose
    public int status;
    @SerializedName("msg")
    @Expose
    public String message;
    @SerializedName("data")
    @Expose
    public User user;
}
尝试以下方法:

 @Override
public void onResponse(Call<HttpResponseModel> call, Response<HttpResponseModel> response) {
    HttpResponseModel httpResponseModel = response.body();
    if (httpResponseModel != null) {
        if (httpResponseModel.getResponse() != null) {
            if (!httpResponseModel.getResponse().isJsonNull()) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<User>() {
                    }.getType();
                    User user = gson.fromJson(httpResponseModel.getResponse(), type);

                    }
                }
            }
    }
@FormUrlEncoded
    @POST("user/login")
    Call<LoginResponse> userLogIn(@FieldMap Map<String, String> credentialMap);
     Map<String, String> credMap = new HashMap<>();
     credMap.put("Username", "vishal.halani");
     credMap.put("Password", "vishal1212");
创建用户名和密码的映射,如下所示:

 @Override
public void onResponse(Call<HttpResponseModel> call, Response<HttpResponseModel> response) {
    HttpResponseModel httpResponseModel = response.body();
    if (httpResponseModel != null) {
        if (httpResponseModel.getResponse() != null) {
            if (!httpResponseModel.getResponse().isJsonNull()) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<User>() {
                    }.getType();
                    User user = gson.fromJson(httpResponseModel.getResponse(), type);

                    }
                }
            }
    }
@FormUrlEncoded
    @POST("user/login")
    Call<LoginResponse> userLogIn(@FieldMap Map<String, String> credentialMap);
     Map<String, String> credMap = new HashMap<>();
     credMap.put("Username", "vishal.halani");
     credMap.put("Password", "vishal1212");
电话如下:

 @Override
public void onResponse(Call<HttpResponseModel> call, Response<HttpResponseModel> response) {
    HttpResponseModel httpResponseModel = response.body();
    if (httpResponseModel != null) {
        if (httpResponseModel.getResponse() != null) {
            if (!httpResponseModel.getResponse().isJsonNull()) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<User>() {
                    }.getType();
                    User user = gson.fromJson(httpResponseModel.getResponse(), type);

                    }
                }
            }
    }
@FormUrlEncoded
    @POST("user/login")
    Call<LoginResponse> userLogIn(@FieldMap Map<String, String> credentialMap);
     Map<String, String> credMap = new HashMap<>();
     credMap.put("Username", "vishal.halani");
     credMap.put("Password", "vishal1212");

请阅读share You json responsesee的第一个可能的副本查看此示例。您在哪里传递用户名和密码???@Body User此模型不包含您的用户名和密码文件请键入答案。如果您仍然难以理解,请告诉我。java.lang.IllegalStateException:预期的开始对象,但s BEGIN_数组第1行第61列路径$。数据{状态:1,消息:您已成功登录,数据:[]}