一. 使用URL对象请求网络
1、HttpURLConnection对象:
//GET : http://192.168.12.37:8080/xxx?a=aaa&b=bbb
//POST : http://192.168.12.37:8080/xxx
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
2、设置请求头信息:GET(GET、POST)
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
3、接收服务器端返回的响应数据,响应码:200 ok,404没有找到资源 ,503服务器端内部错误
int code = conn.getResponseCode();
if(code == 200){
InputStream is = conn.getInputStream();
}
GET和POST请求的请求参数设置不同:
GET请求参数直接用?拼接到地址后面
http://192.168.12.37:8080/xxx?a=aaa&b=bbb
POST请求参数
// 设置请求消息头信息
conn.setRequestProperty("Content-type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", reqData.length()+ "");
// 设置成允许发送数据给服务端,并发送数据
conn.setDoOutput(true);
conn.getOutputStream().write(reqData.getBytes());
二. HttpClient对象
GET方式:
final String path = "http://192.168.12.37:8080/androidServer01_login/servlet/ClientController"+ "?qqAccount="+ URLEncoder.encode(qqAccount)+ "&pwd="+ URLEncoder.encode(pwd); new Thread() {
public void run() {
// 1.获取一个代表浏览器的对象
HttpClient client = new DefaultHttpClient();
// 2.封装网址和请求参数到GET请求对象中
HttpGet httpGet = new HttpGet(path);
// 3.发送请求并接收响应数据
try {
HttpResponse response = client.execute(httpGet);
// 获取响应状态吗
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
//调用工具类方法,将输入流转换成字符串
String res = ReadStreamUtil.stream2String(is);
Message msg = Message.obtain();
msg.obj = res;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}; }.start();
POST方式
final String path = "http://192.168.12.37:8080/androidServer01_login/servlet/ClientController";
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("qqAccount", qqAccount));
parameters.add(new BasicNameValuePair("pwd", pwd));
try {
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(
parameters, "utf-8");
httpPost.setEntity(encodedFormEntity);
HttpResponse response = client.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
String res = ReadStreamUtil.stream2String(is);
Message msg = Message.obtain();
msg.obj = res;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
三. 使用AsyncHttpClient对象请求网络(先引入第三方开源项目)
GET方式
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get("http://192.168.1.1:8080/xxx?a=aa&b=bb", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(getApplicationContext(),
new String(responseBody), 0).show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(),
new String(responseBody), 0).show();
}
});
POST方式
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("qqAccount", qqAccount);
params.add("pwd", pwd);
asyncHttpClient.post("http://192.168.1.1:8080/xxx", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(getApplicationContext(),
new String(responseBody), 0).show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(),
new String(responseBody), 0).show();
}
});
4.XUtils
5.volley
6.okhttp
ps:文件上传使用AsyncHttpClient
多线程断点下载使用XUtils