改造gson JsonSyntaxException解析问题的开始对象

时间:2017-10-30 05:03:47

标签: android json

错误:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $

POJO:

LoginResponse {
    @SerializedName("status")
    private Integer status;
    @SerializedName("message")
    private String message;
    @SerializedName("data")
    private User user;
}

- 创建Pojo类显示json对象数据的用户详细信息 预期BEGIN_OBJECT但在STRING第2行第1列路径$

 class User {
    @SerializedName("id")
    @Expose
    private String id;
}

我的Json输出如

{
    "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"
    }
}

创建用于改造Api解析的界面

@POST("user/login") Call<LoginResponse> userLogIn(@Body User login);

  • 下面的主要活动代码预期为BEGIN_OBJECT但是第2行第1行为STRING

    APILogin service = ApiClient.getClient()。create(APILogin.class);         用户登录=新用户();         login.setEmail(电子邮件);         login.setPassword(密码);

        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());
            }
        });
    

3 个答案:

答案 0 :(得分:1)

将HttpResponseModel类创建为fllow。

HttpResponseModel.java

public class HttpResponseModel {

    @SerializedName("status")
    @Expose
    public int status;
    @SerializedName("msg")
    @Expose
    public String message;
    @SerializedName("data")
    @Expose
    public User user;
}

你的Retrofit onResponse就像

 @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);

                    }
                }
            }
    }

答案 1 :(得分:0)

  

JSON string you can tell from the error message that it is not the correct structure to be parsed into your Ad class.

从你的错误中我可以看出,解析你的班级不是正确的结构。

Gson期望您的JSON字符串以对象左大括号开头。例如 { 未使用 "

  

从后端使用正确的结构制作JSON响应

答案 2 :(得分:0)

尝试如下:

@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");

如下调用:

 Call<LoginResponse> call = apiService.userLogIn(credMap);
call.enqueue(new Callback<LoginResponse>() {
            @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {


            if (response.isSuccessful()) {


LoginResponse loginResponse= ((LoginResponse) response.body());

            }



        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            // Log error here since request failed
            Log.e(TAG, t.toString());


        }
    });