简体   繁体   中英

Unable to fetch the response.body()

I try to create login and register form in Android with retrofit and okhttp3. When I click Button register, the data successful inserted in my database but my toast did not show and it's not automatically direct to login page. Problem is the same on the button login. If I click login button, its not direct to another page. There's no problem on rest API. I think the problem is in the Callback or response.body(). I use restful CodeIgniter.

Here's the code:

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();

                         }
                     }
            );

And here's the 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)

You should check

response.code()

To check the status of your request and let me know to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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