我有一个这种格式的网址"http://testserver.com/Test/login!checkUser.cc?email=testemail123@gmail.com"
。我想将此发送到服务器并且我将得到Json格式"[{"status":"0"}]"
的响应。如何在android中实现这一点。
答案 0 :(得分:1)
您可以使用HttpClient
和其他Http*
类。
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://testserver.com/Test/login!checkUser.cc?email=testemail123@gmail.com");
HttpResponse response = client.execute(request);
// Check if server response is valid
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != 200) {
throw new IOException("Invalid response from server [" + status.toString() + "]");
}
// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
//return result from buffered stream
String dataAsString = builder.toString();
JSONObject jsonObject = new JSONObject(dataAsString);