因为Android中集成了HttpClient,所以我们可以直接使用HttpClient来进行网络编程。使用HttpClient直接加载一个图片实在是有点杀鸡用牛刀,因为HttpClient还可以实现用户登陆,维持和服务器之间的Session。不过为了简单期间,我们还是实现一个如上面的下载图片并显示在ImageView中的例子,还是以Get为例。
HttpClient使用起来同样是发送Request,接收response。
注:由于我的开发环境中Android自带的是3.X的httpClient。上面讲的例子是4.3的httpClient,API有所区别。如果需要使用最新的httpClient,可以自行下载jar包导入
下载: 里面有个HttpClient for Android 4.3.5 (GA) 版本。不过是源码,自行编译成Jar即可。
如果使用老版本的httpClient问题也不大,只是API不太一样了,下面的示例代码使用的是HttpClient for Android 4.3.5。
示例代码:
使用HttpClients
.createDefault()
;创建居然会报错,使用HttpClients.custom().useSystemProperties().build();正常
见
.tuicool.com/articles/nyyaYne
package com.example.hurlconnectionpic;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
Button bt;
String linuxidcImage =
"http://www.linuxidc.com/img/bd_logo1.png";
// HttpClient client=new DefaultHttpClient();
CloseableHttpClient httpclient = HttpClients.custom().useSystemProperties().build();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
new BackGroundTast().execute(linuxidcImage);
}
});
}
class BackGroundTast extends AsyncTask<String, Void, Bitmap> {
Bitmap bitmap;
HttpURLConnection urlConnection;
InputStream in;
CloseableHttpResponse response1;
@Override
protected Bitmap
doInBackground(String... params) {
HttpGet get =
new HttpGet(params[
0]);
try {
response1 = httpclient.execute(get);
HttpEntity entity1 = response1.getEntity();
if (entity1 !=
null) {
bitmap = BitmapFactory.decodeStream(entity1.getContent());
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
response1.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result !=
null) {
iv.setImageBitmap(result);
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
}
1
代码相对HttpURLConnection变化很小,效果图和HttpURLConnection一致。
Volley使用