HttpClient +thread+handleMessage:
new Thread(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(0);
Bitmap bitmap = getHttpGetBitmap(url);
Message message = Message.obtain();
message.obj = bitmap;
message.what = 1;
handler.sendMessage(message);
handler.sendEmptyMessage(2);
}
}).start();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
pDialog.show();
break;
case 1:
iv1.setImageBitmap((Bitmap) msg.obj);
case 2:
pDialog.dismiss();
break;
}
}
};
/**
* httpClient
*
* @param url
* @return
*/
public static Bitmap getHttpGetBitmap(String url) {
try {
Bitmap bitmap = null;
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = null;
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream is = response.getEntity().getContent();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}
return bitmap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
httpUrlConnection+thread+handler.post:
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bitmap = getNetWorkBitmap(url);
handler.post(new Runnable() {
@Override
public void run() {
iv2.setImageBitmap(bitmap);
}
});
}
}).start();
/**
* httpUrlConnection
*
* @param urlString
* @return
*/
public static Bitmap getNetWorkBitmap(String urlString) {
URL imgUrl = null;
Bitmap bitmap = null;
try {
imgUrl = new URL(urlString);
HttpURLConnection urlConn = (HttpURLConnection) imgUrl.openConnection();
urlConn.setDoInput(true);
urlConn.connect();
InputStream is = urlConn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (MalformedURLException e) {
System.out.println("[getNetWorkBitmap->]MalformedURLException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("[getNetWorkBitmap->]IOException");
e.printStackTrace();
}
return bitmap;
}
AsyncTask+httpclient:
new MyAsyncTask(this).execute(url);
/**
* AsyncTask:
*/
class MyAsyncTask extends AsyncTask<String, Void, byte[]> {
private ProgressDialog pDialog;
public MyAsyncTask(Context context) {
pDialog = new ProgressDialog(context);
pDialog.setIcon(R.mipmap.ic_launcher);
pDialog.setTitle("提示");
pDialog.setMessage("The data is loading...");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
@Override
protected byte[] doInBackground(String... strings) {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(strings[0]);
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
byte[] datas = EntityUtils.toByteArray(response.getEntity());
return datas;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] bytes) {
super.onPostExecute(bytes);
if (bytes != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
iv3.setImageBitmap(bitmap);
}
pDialog.dismiss();
}
}