【问题标题】:Retrofit:2.0.0-beta2 http call and response handlingRetrofit:2.0.0-beta2 http调用和响应处理
【发布时间】:2016-03-04 17:46:02
【问题描述】:

我的后端返回这个 json 对象来表示新用户注册成功 { “错误”:错误, "message": "您已成功注册" }

这表明帐户已经存在 { “错误”:是的, "message": "对不起,这封邮件已经存在" }

这在其他注册失败时 { “错误”:是的, "message": "发生错误!!请重试" }

我想使用 retrofit:2.0.0-beta2 进行异步 http 调用并处理响应以将使用带到主页或相应地重定向到登录页面。

这是我的 POJO: 公共类用户{

public final String email, firstname, lastname, city, birthday, gender, password;

public User(final String email, final String firstname, final String lastname,
            final String city, final String birthday, final String gender, final String password) {
    this.email = email;
    this.firstname = firstname;
    this.lastname = lastname;
    this.city = city;
    this.birthday = birthday;
    this.gender = gender;
    this.password = password;
}

} 这是我的服务: 公共接口 APIService {

@POST("/api/user")
Call<User> createUser(@Body User user);

}

【问题讨论】:

    标签: retrofit


    【解决方案1】:

    您需要创建一个额外的类来处理响应。

    public class ResponseApi{
        public String error;
        public String message;
    }
    

    你是人脸

    @POST("/api/user")
    Call<ResponseApi> createUser(@Body User user);
    

    【讨论】:

      【解决方案2】:

      您需要实现onResponse 侦听器并在其中处理重定向或任何其他操作。

      例如:

        protected void loginAttempt(final View v) {
          authUser = new HashMap<>();
          authUser.put("email", email.getText().toString());
          authUser.put("password", password.getText().toString());
      
          Call call = SubApplication.api.authUser(authUser);
          call.enqueue(new Callback() {
              @Override
              public void onResponse(Call call, Response response) {
                  try {
                      JSONObject responseObject = new JSONObject(new Gson().toJson(response.body()));
                      Log.d("LoginActivity", "onResponse: " + responseObject.toString());
      
                      if (responseObject.getBoolean("success") == true) {
                          Toast.makeText(v.getContext(), getString(R.string.error_login_success), Toast.LENGTH_LONG).show();
                          Intent intent = new Intent(v.getContext(), DashboardActivity.class);
                          startActivity(intent);
                          finish();
                      } else {
                          Toast.makeText(v.getContext(), getString(R.string.error_login_failed), Toast.LENGTH_LONG).show();
                      }
                  } catch (Exception e) {
                      Log.e("LoginActivity", "Exception: " + e);
                      Toast.makeText(v.getContext(), getString(R.string.error_login_failed), Toast.LENGTH_LONG).show();
                  }
              }
      
              @Override
              public void onFailure(Call call, Throwable t) {
                  Log.e("LoginActivity", "onFailure: " + t);
              }
          });
      

      【讨论】:

        猜你喜欢
        • 2020-04-26
        • 1970-01-01
        • 2016-03-22
        • 2020-05-14
        • 2018-12-27
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        相关资源
        最近更新 更多