我在YouTube for Retrofit中遵循此tutorial的要求,用于在发布请求中发送对象,但是在模拟器上遇到错误,其onResponse方法的响应为空。
问题:
主要活动:
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();
}
});
}
}
NOT NPE证明:
UserClient:
public interface UserClient {
@POST("api/users")
Call<User> createAccount(@Body 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;
}
节点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;
我尝试过的
我尝试在终端上使用“ ipconfig”将BASE_URL更改为计算机的本地IP地址。 ='http://192.168.X.X:3000/api/users/'
在UserClient = @Headers(“ Content-Type:application / json”)中使用标头和FormURLEncoded | @FormUrlEncoded。
使用邮递员检查回复,成功完成。
检查了Robomongo以进行验证/
邮差:
获取请求
发布请求
Robomongo
最佳答案
BaseURL应该为:baseUrl("http://10.0.2.2:3000/")
,因为您的UserClient
接口具有路径:
@POST("api/users")
Call<User> createAccount(@Body User user);
更新
您的主题字段应为
String
而不是String[]
更改此:
private String [] topics;
对此:
private String topics;