一、概述
在Android 上发送HTTP 请求的方式一般有两种:HttpURLConnection 和HttpClient。因为在Android 5.0之后,HttpClient被HttpURLConnecetion替代,后来在Android 6.0完全被舍弃,所以本文重点讲解HttpURLConnecetion。
二、HttpURLConnecetion的使用步骤
(1)首先需要获取到HttpURLConnection 的实例,一般只需new 出一个URL 对象,并传入目标的网络地址,然后调用一下openConnection()方法即可,如下所示:
URL url = new URL("");
HttpURLConnection connection = (HttpURLConnection) ();
(2)得到了HttpURLConnection 的实例之后,我们可以设置一下HTTP 请求所使用的方法。常用的方法主要有两个,GET 和POST。GET 表示希望从服务器那里获取数据,而POST 则表示希望提交数据给服务器。写法如下:
httpURLConnection.setRequestMethod("GET");
(3)接下来就可以进行一些*的定制了,比如设置连接超时、读取超时的毫秒数,以及服务器希望得到的一些消息头等。这部分内容根据自己的实际情况进行编写,示例写法如下:
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
(4)之后再调用getInputStream()方法就可以获取到服务器返回的输入流了,剩下的任务就是对输入流进行读取,如下所示:
InputStream in = connection.getInputStream();
(5)最后可以调用disconnect()方法将这个HTTP 连接关闭掉,如下所示:
connection.disconnect();
三、HttpURLConnecetion的使用范例
为简单起见,还是在Android之AsyncTask详解基础上修改,实现下载图片的功能,原本使用HttpClient实现,我们换做HttpURLConnecetion来实现。
package ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends AppCompatActivity {
private ProgressDialog progressDialog;//下载进度条
private Button button;
private ImageView imageView;
//图片下载地址
private final String IMAGE_URL = "/image/pic/item/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(.activity_main);
button = (Button) findViewById();
(new () {
@Override
public void onClick(View v) {
new MyAsyncTask().execute(IMAGE_URL);
}
});
imageView = (ImageView) findViewById();
progressDialog = new ProgressDialog(this);
("提示");
("正在下载,请稍后...");
(ProgressDialog.STYLE_HORIZONTAL);
}
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
// 显示进度对话框
();
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
HttpURLConnection connection = null;
try {
//第一步,获取到HttpURLConnection 的实例
URL url = new URL(params[0]);
connection = (HttpURLConnection) ();
//第二步,设置一下HTTP 请求所使用的方法GET ,从服务器那里获取数据
("GET");
//第三步,设置连接超时、读取超时
(8000);
(8000);
int code = ();
if (code == 200) {
//第四步,获取到服务器返回的输入流,并进行读取
inputStream = ();
long file_len = ();
int len = 0;
byte[] data = new byte[1024];
int total_len = 0;
while ((len = (data)) != -1) {
total_len += len;
int value = (int) ((total_len / (float) file_len) * 100);
//反馈当前任务的执行进度
publishProgress(value);
(data, 0, len);
}
byte[] result = ();
bitmap = (result, 0, );
}
} catch (Exception e) {
();
} finally {
//第五步,将这个HTTP 连接关闭掉
if(connection != null){
();
}
if (inputStream != null) {
try {
();
} catch (IOException e) {
();
}
}
}
return bitmap;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//设置进度条刻度
(values[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//关闭进度条
();
//显示图片
(bitmap);
}
}
}
运行结果显示,详见Android之AsyncTask详解的运行结果。