1
POST沒有更新,我需要一個簡單的POST方法使用改裝,槽我的REST的API建立在節點,快速和貓鼬,結束了在我Mongolab接待了來自我的Android應用會D b。問題是我沒有得到任何錯誤...不是從改造,也不是從我的後端。但沒有添加到我的分貝。我想在我的項目中添加一個定義爲POJO的'用戶'。我的getall和findbyid方法都能正常工作,所以我必須在發佈時做錯了任何幫助,我們將不勝感激! :)分貝的使用改造2,REST API和MongoDB
用戶POJO:
public class User {
@SerializedName("_id")
@Expose
private String id;
@SerializedName("emailaddress")
@Expose
private String emailaddress;
@SerializedName("name")
@Expose
private String name;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("password")
@Expose
private String password;
@SerializedName("vegetarianExperience")
@Expose
private Integer vegetarianExperience;
@SerializedName("familySituation")
@Expose
private Integer familySituation;
@SerializedName("newsletter")
@Expose
private boolean newsletter;
@SerializedName("score")
@Expose
private int score;
@SerializedName("challenges")
@Expose
private List<String> challenges = new ArrayList<>();
@SerializedName("favoriteRecipes")
@Expose
private List<String> favoriteRecipes = new ArrayList<>();
public User(String id, String emailaddress, String name, String lastName,
String password, Integer vegetarianExperience, Integer familySituation,
boolean newsletter, int score, List<String> challenges, List<String> favoriteRecipes) {
this.id = id;
this.emailaddress = emailaddress;
this.name = name;
this.lastName = lastName;
this.password = password;
this.vegetarianExperience = vegetarianExperience;
this.familySituation = familySituation;
this.newsletter = newsletter;
this.score = score;
this.challenges = challenges;
this.favoriteRecipes = favoriteRecipes;
}
/*getters and setters*/
}
的usermodel在後臺與貓鼬:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var UserSchema = new Schema({
_id: ObjectId,
emailaddress: String,
name: String,
lastName: String,
password: String,
vegetarianExperience: Number,
familySituation: Number,
newsletter: Boolean,
score: Number,
challenges: [
{
type: Schema.ObjectId, ref: 'Challenge'
}
],
favoriteRecipes: [
{
type: Schema.ObjectId, ref: 'Recipe'
}
]
});
module.exports = mongoose.model('User', UserSchema);
我在Java客戶端類:從我的應用程序的創建inputfields
public class RestClient {
private static ApiInterface apiInterface;
private static String baseUrl = "http://188.166.128.173:8080/api/" ;
public static ApiInterface getClient() {
if (apiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiInterface = client.create(ApiInterface.class);
}
return apiInterface;
}
public interface ApiInterface {
@GET("recipes")
Call<List<Recipe>> recipes();
@GET("users")
Call<List<User>> userByMail(@Query("emailaddress") String emailaddress);
@POST("users")
Call<User> registerNewUser(@Body User user);
}
}
用戶和調用api的方法:
個private void registerNewUser(User user) {
RestClient.ApiInterface service = RestClient.getClient();
Call<User> registerNewUser = service.registerNewUser(user);
registerNewUser.enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
if (response.isSuccess()) {
Log.d("MainActivity", "User is gemaakt!!" + response.body());
goToChallengeBoard();
} else {
Log.d("Error: ", response.errorBody().toString());
}
}
@Override
public void onFailure(Throwable t) {
Log.d("MainActivity", "GOE KAPOT " + t.getMessage());
}
});
}
我app.js在後端:
var User = require('./app/models/user');
router.route('/users')
// create a user (accessed at POST http://localhost:8080/api/users)
.post(function(req, res) {
var user = new User(req.body); // create a new instance of the User model
// save the user and check for errors
user.save(function(err) {
if (err){
return res.send(err);
}
res.json({ message: 'User created!' });
});
})
// get all the users (accessed at GET http://localhost:8080/api/users)
.get(function(req, res) {
User.find(function(err, users) {
if (err)
res.send(err);
res.json(users);
});
});