Android上的HTTP Client
Android提供了两个HTTP Client:AndroidHttp Client和HttpUrlConnection
AndroidHttp Client来自apache HTTPClient
关于HTTPClient的使用可以参考官网.
HttpUrlConnection
HttpUrlConnection的使用比较简单,不需要导入第三方包
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://www.android.com/");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // Adjust if you want
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
JSONObject resultJSON = new JSONObject(outputStream.toString());
}catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
Volley框架的网络请求也是通过HttpUrlConnection来实现的,关于Volley的使用可参考smanikandan14