您在尋找HttpGet,HttpResponse,httpRequest和DefaultHttpClient對象,這些對象允許您發送Web請求並獲取結果以用於Web服務。
參見:http://developer.android.com/reference/org/apache/http/HttpRequest.html
protected InputStream callSynchronousUrl(final String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter("http.socket.timeout",
3000);
URI uri = new URI(url);
HttpGet requestObject = new HttpGet();
requestObject.setURI(uri);
HttpResponse response = httpClient.execute(requestObject);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return(null);
}
else {
//We successfully got a response from the server
return(response.getEntity().getContent());
}
} catch (IOException e) {
Log.e("CallUrl", "I/O error");
return(null);
} catch (URISyntaxException e) {
Log.e("CallUrl", "URI syntax is invalid");
return(null);
}
}