【发布时间】:2014-09-23 18:15:09
【问题描述】:
使用下面的代码,我可以从 login/json 中获取 json 信息。
如何将其转换为我可以使用的东西? 我现在正在尝试确定用户是否存在。 如果没有返回:
`{
"user": null,
"sessionId": null,
"message": "Invalid email/username and password"
}
任何指导都会很棒。 `
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost("http://localhost:9000/auth/login/json");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("user", "user"));
nameValuePair.add(new BasicNameValuePair("pw", "password"));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
// writing response to log
Log.d("Http Response:", response.toString());
System.out.println(EntityUtils.toString(entity));
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
【问题讨论】: