使用下面的代碼我能夠從登錄/ json獲取json信息。使用來自Http的響應get 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();
}
我將如何使用null?我可以通過做if(user = null)來使用它嗎? – Gchorba 2014-09-23 18:55:36
修改了帖子 – 2014-09-23 18:59:57