我有一个这种格式的 URL "http://testserver.com/Test/login!checkUser.cc?email=testemail123@gmail.com"
。我想把它发送到服务器,然后我会得到 Json 格式的响应"[{"status":"0"}]"
。如何在android中实现这一点。
问问题
398 次
1 回答
1
您将使用 anHttpClient
和其他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);
于 2013-02-10T05:39:15.243 回答