Original Article: http://brainflush.wordpress.com/2008/10/17/talking-to-web-servers-via-http-in-android-10/
HttpClient httpClient = new DefaultHttpClient();
StringBuilder uriBuilder = new StringBuilder(SERVICE_ENDPOINT);
uriBuilder.append("?param0=" + param0);
uriBuilder.append("¶m1=" + param1);
uriBuilder.append("¶mN=" + paramN);
HttpGet request = new HttpGet(uriBuilder.toString());
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
// we assume that the response body contains the error message
if (status != HttpStatus.SC_OK) {
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
response.getEntity().writeTo(ostream);
Log.e("HTTP CLIENT", ostream.toString()));
} else {
InputStream content = response.getEntity().getContent();
// <consume response>
content.close(); // this will also close the connection
}
------
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(SERVICE_ENDPOINT);
MultipartEntity entity = new MultipartEntity();
entity.addPart("param0", new StringBody("value0"));
entity.addPart("param1", new StringBody(Double.toString(1.0)));
entity.addPart("paramN", new FileBody(new File("/bar/baz")));
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) {
// see above
} else {
// see above
}
全站熱搜
留言列表