1

I was following this tutorial in YouTube for Retrofit for sending objects in a post request but encounter an error on emulator with a null response onResponse method.

Problem: Null Response on Response

Main Activity:

public class MainActivity extends AppCompatActivity {


private EditText mName, mEmail, mAge, mTopics;
private Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mName = findViewById(R.id.editText);
    mEmail = findViewById(R.id.editText2);
    mAge = findViewById(R.id.editText3);
    mTopics = findViewById(R.id.editText4);
    mButton = findViewById(R.id.button);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            User user = new User(
                    mName.getText().toString(),
                    mEmail.getText().toString(),
                    Integer.parseInt(mAge.getText().toString()),
                    mTopics.getText().toString().split(",")
            );

            //Added proof its not a NPE!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
            Log.d("Main", mName.getText().toString() +
                            mName.getText().toString()+ Integer.parseInt(mAge.getText().toString()) +
                            Integer.parseInt(mAge.getText().toString()) +mTopics.getText().toString().split(","));

            sendNetworkRequest(user);
        }
    });

}

private void sendNetworkRequest(User user) {
    Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("http://10.0.2.2:3000/api/users/")
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    UserClient client = retrofit.create(UserClient.class);
    Call<User> call = client.createAccount(user);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            Toast.makeText(MainActivity.this, "Success UserID = "+ response.body(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
        }
    });

}

}

Proof of NOT NPE:

NOT NPE

UserClient:

public interface UserClient {

    @POST("api/users")
    Call<User> createAccount(@Body User user);
}

User:

public class User {

private Integer id;
private int age;
private String name, email;
private String [] topics;

public Integer getId() {
    return id;
}

public User(String name, String email, int age,  String[] topics) {
    this.age = age;
    this.name = name;
    this.email = email;
    this.topics = topics;
}

Node.JS

App.js

const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./routes/api');
const mongoose = require('mongoose');

const app = express();

mongoose.connect('mongodb://localhost/user1',  { useMongoClient: true });
mongoose.Promise = global.Promise;


app.use(bodyParser.json());
app.use('/api', routes);

app.listen(process.env.port || 3000, function(){
    console.log('Using PORT 3000');
});

Api.js:

const express = require('express');
const router = express.Router();
const User = require('../models/user');

router.get('/users', function(req, res, next){
    res.send({type: "GET"});
});

router.post('/users', function(req, res, err){
    User.create(req.body).then(function(user){
        res.send(user);
    });
});

module.exports = router;

User.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    name: {
        type: String
    },
    email: {
        type: String
    },
    age: {
        type: Number
    },
    topics: {
        type: String
    }
});

const User = mongoose.model('user', UserSchema);

module.exports = User;

What I have tried:

  • I have tried changing the BASE_URL the local ip address of my machine using 'ipconfig' on my terminal. = 'http://192.168.X.X:3000/api/users/'
  • Use Headers and FormURLEncoded in the UserClient = @Headers("Content-Type: application/json") | @FormUrlEncoded.
  • Used postman to check responses, which successfully happen.
  • Checked Robomongo for verification/

Postman:

Get Request Get Request

Post Request Post Request

Robomongo Robomongo

Ispam
  • 495
  • 7
  • 18

2 Answers2

1

BaseURL should be: baseUrl("http://10.0.2.2:3000/") because your UserClient interface has the path:

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

UPDATE

Your Topic field should be String not String[]

Change this:

private String [] topics;

To this:

private String topics;
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
0

Maybe you are doing a bad request?

Try to add a check for null in onResponse() and access the raw response like this response.errorBody.string()

zemaitis
  • 203
  • 2
  • 12
  • Added `if (response.body() == null){ Log.e("Error",response.code()+""); Log.e("Erro2", response.errorBody().toString()); return; }` and got `E/Error: 404 E/Erro2: okhttp3.ResponseBody$1@35d2a64c` – Ispam Dec 05 '17 at 19:49