为什么翻新会忽略成功的响应代码?

时间:2019-04-16 18:14:00

标签: android api retrofit

我是在Android上使用Restful API的新手,正在使用Retrofit将API连接到我的Android应用程序,我想做的就是将用户信息插入到PostgreSQL数据库中,但是每次我插入新数据时,尽管成功插入并返回了代码200,我的应用仍会忽略onResponse方法并直接进入onFailure,我该如何解决呢?

我已经使用HttpLoggingInterceptor记录了错误,但是它显示了代码200,这意味着从服务器端来看一切正常。

改装服务类(UserService)

public interface UserService {
    //Return list of users
    @GET("/users")
    Call<List<User>> getUsers();
    //Post new user
    @POST("/users")
    Call<User> postUser(@Body User user);
}

用户类别

public class User {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("email")
    @Expose
    private String email;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

改造连接和HttpLogging实例

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        httpClient.addInterceptor(logging);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

触发在MainActivity上插入的FAB

FloatingActionButton fab = findViewById(R.id.fab);
        final User user = new User();
        user.setName("Michael");
        user.setEmail("michael@example.com");
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userService.postUser(user).enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        if(response.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "User successfully inserted", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(MainActivity.this, "Error inserting into database", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

2 个答案:

答案 0 :(得分:0)

可能是因为转换器引发了异常。在onFailure方法中打印抛出错误消息。如果仅仅是转换器和线路数据之间的分歧,那就是需要解决的应用程序问题。

请参阅更多

https://github.com/square/retrofit/issues/1446

看到截图后更新

这是一个解析错误。作为响应,它期望使用用户类型的JSON对象,但是当前响应返回的是JSON字符串。

答案 1 :(得分:0)

I found the problem after all.

I was sending a JSON response from my API, which was incorrectly formatted, and Retrofit was taking that as an error.

Here's a screenshot of my new API function: http://prntscr.com/ncyxwx