14 thg 3, 2012

[Networking Tut] Làm việc với networking sử dụng Thread và AsyncTask

Làm việc trên môi trường network với  Thread và AsyncTask giúp cho ứng dụng xử lý tốt hơn trong khi đang load data về từ internet.
Các bạn kham khảo các vấn đề cần quan tâm trong bài này là :
Thread in android , đối tượng Handler, và lớp AsyncTask . và cách thức làm việc của các lớp này.
Hoàn thành bài này sẽ có kết quả như sau :


Class StringLoaderTask lấy data kiểu String extends từ lớp AsyncTask như sau :

private class StringLoaderTask extends AsyncTask<String, Integer, String>{

@Override
protected String doInBackground(String... params) {
String result = null;
if (params.length <= 0) {
return null;
}
for (int i = 0; i < params.length; i++) {
String aURL = params[i];
try {
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet getobj = new HttpGet(aURL);
HttpResponse response = httpclient.execute(getobj);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity anEntity = response.getEntity(); 
Header aHeader = anEntity.getContentType();
if (aHeader.getValue().contains("text/")) {
result = EntityUtils.toString(anEntity);
}
}
} catch (ClientProtocolException e) {
Log.e(LOG_TAG, "Falied at ClientProtocolException with error: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Falied at IOException with error: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e(LOG_TAG, "Falied at Exception with error: " + e.getMessage());
e.printStackTrace();
}
}
return result;
}

@Override
protected void onPostExecute(String result) {
if (result != null && result.length() > 0) {
mOutputContent.setText(result);
}
}
}

Class BitmapLoaderTask lấy data kiểu Bitmap extends từ lớp AsyncTask như sau : 

private class BitmapLoaderTask extends AsyncTask<String, Integer, Bitmap>{

@Override
protected Bitmap doInBackground(String... params) {
Bitmap result = null;
if (params.length <= 0) {
return null;
}
for (int i = 0; i < params.length; i++) {
String aURL = params[i];
try {
HttpClient httpclient = (HttpClient) new DefaultHttpClient();
HttpGet getobj = new HttpGet(aURL);
HttpResponse response;
response = httpclient.execute(getobj);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity anEntity = response.getEntity(); 
Header aHeader = anEntity.getContentType();
if (aHeader.getValue().contains("image/")) {
result = new BitmapDrawable(anEntity.getContent()).getBitmap();
}
}
} catch (ClientProtocolException e) {
Log.e(LOG_TAG, "Falied at ClientProtocolException with error: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Falied at IOException with error: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
Log.e(LOG_TAG, "Falied at Exception with error: " + e.getMessage());
e.printStackTrace();
}
}
return result;
}

@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
mImageView.setImageBitmap(result);
}
}
}




Link download project : tại đây






1 nhận xét:

  1. Cảm ơn nhiều. Cách này load hình đỡ chậm hẵn. :D Thank bạn.

    Trả lờiXóa

Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...