我在Android和Android中相对较新宁静的API编程,所以我可能会犯一些愚蠢的错误。但是我从JsonObject的回复中面对这个:
对于值\"登录\",转换为ObjectId失败在路径\" _id \" for model \" User \""," name":" CastError"," stringValue":" \ "登录\"""种类":"的ObjectId""值":"登录" "路径":" _id"}
我在Postman上测试了我的Rest API,一切都恢复正常。 Restful API是用Node.js和Express编写的。
Rest API假设返回状态,用户ID和消息,以便让应用知道用户是否使用正确的用户名和密码登录。登录工作正常,它返回200,允许应用程序登录并使用会话登录。但是,我无法从响应JsonObject获取任何内容以获取用户ID,以显示用户信息的下一步。
(目标Android API 23)
代码:
Restful.Java
public class RestClient {
private static final String BASE_URL = "http://10.0.2.2:8000/api/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl){
Log.d("URL: ", BASE_URL+relativeUrl);
return BASE_URL + relativeUrl;
}
}
LoginActivity.Java(仅限登录部分)
public void checkLoginDB(final RequestParams params, final String username){
prgDialog.show();
RestClient.post("user/login", params, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
Log.d("StatusCode: ", "Code "+ statusCode);
try {
if (statusCode == 200) {
Toast.makeText(getApplicationContext(), "Logged In!", Toast.LENGTH_LONG).show();
session.createLoginSession(username);
Log.d("Log: ", "what " + response);
navigatetoHomeActivity();
}
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Log.d("StatusCode: ", "Code "+ statusCode);
prgDialog.hide();
Toast.makeText(getApplicationContext(), "Failed!", Toast.LENGTH_LONG).show();
}
});
}
Restful API的Server.js
router.post('/user/login', function(req, res){
var username = req.body.username;
var password = req.body.password;
if(username.length > 0 && password.length > 3){
User.findOne({username: username, password: password}, function(err, user){
if(err)
res.json({statusCode: 0, message: "login error: " + err});
if(!user)
res.json({statusCode: 0, message: "Not Found"});
res.json({statusCode: 200, userid: user._id, message: "Login Sucessful"});
})
} else {
res.json({statusCode: 0, message: "Invalid Fields"});
}
});
app.use('/api', router)
User.js(已定义架构)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String,
firstname: String,
lastname: String,
agentID: Number,
email: String,
phone: Number
});
module.exports = mongoose.model('User', UserSchema);
答案 0 :(得分:0)
每当您想要登录用户并希望返回时使用post()方法的用户详细信息。
在您的代码中,第二种方法不会发布,因此请将client.get()
更改为client.post()
喜欢这个public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
client.post(getAbsoluteUrl(url), params, responseHandler);
}