简体   繁体   中英

Receiving NULL on response error body if the status code is 400 with retrofit in android

I was working on some login process and got stuck. The problem was when i enter a wrong password it always throws the NullPointerException because my getMessage was receiving null response from backend. After that what i did was i changed my status code in node from 400 to 200 (200 - which i am using for success status) and then tried same procedure, After entering wrong password this time it gave me the response from the node.

Which means my retrofit is not receiving response for status code 400 and when changed it to 200 it worked well that was really weird.

Anyone know why this is happening if you want me to post my login or node code i can.

THANKS !

EDIT

This is my onResponse method in login activity.

 public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            ServerResponse resp = response.body();

            if (resp.getmessage().equals(Constants.SUCCESS))
            {
                Toast.makeText(Login.this, resp.getmessage(), Toast.LENGTH_SHORT).show();
                goToProfile();

        }
        else {
                Toast.makeText(Login.this, resp.getmessage(), Toast.LENGTH_SHORT).show();
            }

        }

and here is my node code, currently all status codes are 200 but when i used 400 for wrong password and user not fount it returns null.

const User= require("../model/user.model.js");
const { initSession} = require('../utils/utils');

const logins =  (req,res,next)=> {  
console.log("login user::"+JSON.stringify(req.body));
console.log("password:::"+req.body.user.password);  
 User.findOne({ username : req.body.user.username }, function(err, user) {
    if (!user) {           
          return res.status(200).send({
              message : "User not found."
          });
      }    
      else {    

        if (user.validPassword(req.body.user.password)) {   
            console.log("login successfully!!!");            
            return res.status(200).send({
                message : "success"
            });

        }        
    else if(!(user.validPassword(req.body.user.password))) {
        console.log("wrong password");
        return res.status(200).send({
            message : "Wrong Password"
        });
    }
}
  })
  }

module.exports = { logins };

When you receive error retrofit isn't parsing it but passing it as an errorBody.

So you need to parse this json: response.errorBody().string() to your ServerResponse object and then you can access message property.

Edit:

Your response function:

public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
    if(response.isSuccessful()) {
        ServerResponse serverResponse = response.body()
        if(serverResponse.getMessage().equals(Constants.SUCCESS)) {
            Toast.makeText(Login.this, response.body().getMessage()).show();
            goToProfile();
        } else {
            Toast.makeText(Login.this, serverResponse.getMessage(), Toast.LENGTH_SHORT).show();
        }
    } else {
        Gson gson = new Gson();
        ServerResponse errorResponse = gson.fromJson(response.errorBody().string(), ServerResponse.class);
        Toast.makeText(Login.this, errorResponse.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

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