Android Loopj的Async JsonObject reponse被强制转换为Objectid失败



我在安卓系统中相对较新;Restful API编程,所以我可能会犯一些愚蠢的错误。然而,我面临着来自我的回应JsonObject:

对于模型"User\"的路径"_id\"处的值"login\","name":"CastError","stringValue":"\"login\","kind":"ObjectId","value":"login","path":"_id"},强制转换为ObjectId失败

我已经在Postman上测试了我的Rest API,一切都很好。Restful API是用Node.js和Express编写的。

Rest API将返回一个状态、用户ID和消息,让应用程序知道用户是否使用正确的用户名和密码登录。登录操作正常,它会给我返回200,允许应用程序登录并使用会话保持登录状态。但是,我无法从响应JsonObject中获得任何信息来获取下一步显示用户信息的userid。

(目标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;
}
}

LoginActivityJava(仅登录部分)

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);

每当您想登录用户并希望返回用户详细信息时,请使用post()方法。在您的代码中,第二种方法不是post,所以将client.get()更改为client.post()

像这个public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){ client.post(getAbsoluteUrl(url), params, responseHandler); }

相关内容

  • 没有找到相关文章

最新更新