如何在android中将参数传递给服务器

时间:2013-02-10 05:05:53

标签: android json webserver

我有一个这种格式的网址"http://testserver.com/Test/login!checkUser.cc?email=testemail123@gmail.com"。我想将此发送到服务器并且我将得到Json格式"[{"status":"0"}]"的响应。如何在android中实现这一点。

1 个答案:

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