繁体   English   中英

Android call.enqueue改造

[英]Android call.enqueue Retrofit

我有这个代码。 我的输入数据(调用过程)已成功插入数据库,但是我没有使用call.enqueue获得返回数据。 它给了我“失败”的烤面包,而不是“成功”的烤面包。 我正在使用改造2.1.0。 这是我的进口货:

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

这是我的代码

public void performRegistration(){
    final String name = uname.getText().toString();
    final String email = mail.getText().toString();
    final String pw1 = password1.getText().toString();
    final String pw2 = password2.getText().toString();

    Call<User> call = MainActivity.apiInterface.performRegistration(name, email, pw1);

    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            MainActivity.prefConfig.displayToast("success");
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            MainActivity.prefConfig.displayToast("Failed");
        }
    });
}

onResponse处理来自服务器的所有响应。 当您获得状态码404或500时,将调用此方法。 您可以使用response.code()检查状态代码。 当您的应用程序无法连接到服务器时,调用onFailure。

这可能由于某些原因而发生。 1)没有可用的互联网。 2)无法连接到服务器。 请使用e.printstack()发布您正确的响应错误; 谢谢..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM