【问题标题】:How to parse json with retrofit and gson asynchronously如何使用改造和 gson 异步解析 json
【发布时间】:2016-08-23 10:45:35
【问题描述】:

我有 1 个请求,可以得到 2 个不同的响应:

登录名和密码正确时:

{
    "token": "token..",
    "expires": "2016-04-28T10:46:36+0000"
}

错误时:

{
    "error": {
        "id": "0123456789",
        "code": 401,
        "message": "invalid credentials"
    }
}

虽然更正它解析成功,但是当我得到错误的响应(错误的登录名或密码)时,它返回 null(未解析)。

Error.java

public class Error {
    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("code")
    @Expose
    public int code;
    @SerializedName("message")
    @Expose
    public String message;

    public Error() {}

    public Error(String id, int code, String message) {
        this.id = id;
        this.code = code;
        this.message = message;
    }
}

Auth.java

public class Auth {
    @SerializedName("token")
    @Expose
    public String token;

    @SerializedName("expires")
    @Expose
    public String expires;

    @SerializedName("error")
    @Expose
    public Error error;

    public Auth(String token, String expires, Error error) {
        this.token = token;
        this.expires = expires;
        this.error = error;
    }

    public Auth() {
        error = new Error();
    }
}

ApiRequests.java

public interface ApiRequests {
    @FormUrlEncoded
    @POST("auth/token")
    Call<Auth> auth(@Field("login") String login, @Field("password") String password);

}

Api.java

Gson gson = new GsonBuilder()
        .setDateFormat(DATE_FORMAT)
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(URL_MAIN)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

ApiRequests service = retrofit.create(ApiRequests.class);
Call<Auth> call = service.auth("login", "password");

call.enqueue(new Callback<Auth>() {
    @Override
    public void onResponse(Call<Auth> call, Response<Auth> response) {
        Auth responseBody = response.body(); // null when login/password are wrong
    }

    @Override
    public void onFailure(Call<Auth> call, Throwable t) {

    }
});

build.gradle

dependencies {
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.google.code.gson:gson:2.6.2'
}

【问题讨论】:

    标签: android json gson retrofit retrofit2


    【解决方案1】:

    Auth.java

    public class Auth implements Serializable {
    
        private String token;
        private String expires;
        private Error error;
    
        //Implements getters and setters
    }
    

    错误.java

    public class Error implements Serializable {
    
        private String id;
        private int code;
        private String message;
    
        //Implements getters and setters
    }
    

    API.java

    call.enqueue(new Callback<Auth>() {
        @Override
        public void onResponse(Call<Auth> call, Response<Auth> response) {
            if(response.code() == 401){
                //failure
                Log.e("Error", response.body().getError().getMessage());
            } else{
                //success
                Log.e("Success", response.body().getToken());
            }
        }
    
        @Override
        public void onFailure(Call<Auth> call, Throwable t) {
    
        }
    });
    

    【讨论】:

    • 在“onResponse()”方法“response.body() = null”中添加了“实现可序列化”。 “response.code() = 401”,为什么不是 200?我在“www.hurl.it”上检查了它,服务器返回 JSON 和代码 401
    • 检查您的API,但它意味着身份验证失败
    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2017-09-20
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多