无法获取response.body()

时间:2018-05-17 08:04:58

标签: java android retrofit retrofit2 okhttp

我尝试使用改造和okhttp3在Android中创建登录和注册表单。当我单击按钮注册时,数据成功插入我的数据库但我的吐司没有显示,并且它不会自动指向登录页面。按钮登录时问题是相同的。如果我单击登录按钮,它不会直接到另一个页面。其余的API没有问题。我认为问题出在Callback或response.body()中。我使用restful CodeIgniter。

以下是代码:

mApiService.createUser(
            input_name.getText().toString(),
            input_lastname.getText().toString(),
            input_username.getText().toString(),
            input_email.getText().toString(),
            input_password.getText().toString(),
            input_phonenumber.getText().toString(),
            input_country.getSelectedItem().toString()
    )
            .enqueue(new Callback<ResponseBody>() {
                         @Override
                         public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                             if (response.isSuccessful()) {
                                 Log.i("debug", "onResponse: Successful");
                                 loading.dismiss();
                                 try {
                                     JSONObject jsonRESULTS = new JSONObject(response.body().string());
                                     if (jsonRESULTS.getString("error").equals("true")) {
                                         Toast.makeText(mContext, "Register Successful", Toast.LENGTH_SHORT).show();
                                         startActivity(new Intent(mContext, SignInActivity.class));
                                     } else {
                                         String error_message = jsonRESULTS.getString("error_msg");
                                         Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
                                     }
                                 } catch (JSONException | IOException e) {
                                     e.printStackTrace();
                                 }
                             } else {
                                 Log.i("debug", "onResponse: Register Failed");
                                 loading.dismiss();
                             }
                         }

                         @Override
                         public void onFailure(Call<ResponseBody> call, Throwable t) {
                             Log.e("debug", "onFailure: ERROR > " + t.getMessage());
                             Toast.makeText(mContext, "Connection Failed", Toast.LENGTH_SHORT).show();

                         }
                     }
            );

这里是logcat:

    content-type: application/json; charset=utf-8
05-17 11:02:24.263 23925-23955/com.example.user.pfmapp2 D/OkHttp: {"status":true,"message":"Registration Successfully, Please SignIn.","0":{"name":"test","lastname":"","username":"test","email":"Dddd33@gmail.com","password":"31a30b53d7faec7957e708dca6077e31","registration_date":"17-05-18","mobile_phone_number":"234124445","mobile_phone_number_code":"+1"}}
    <-- END HTTP (313-byte body)
05-17 11:02:24.283 23925-23925/com.example.user.pfmapp2 I/debug: onResponse: Register Failed
05-17 11:02:24.301 23925-23941/com.example.user.pfmapp2 D/EGL_emulation: eglMakeCurrent: 0xa7b18960: ver 2 0 (tinfo 0xa341c6e0)
05-17 11:02:24.321 23925-23941/com.example.user.pfmapp2 D/EGL_emulation: eglMakeCurrent: 0xa7b18960: ver 2 0 (tinfo 0xa341c6e0)

1 个答案:

答案 0 :(得分:0)

你应该检查

response.code()

检查您的请求状态并让我知道为您提供帮助