Untuk men-download sebuah content dari halaman URL membutuhkan waktu koneksi. Jika koneksi internet pada device tidak terlalu cepat, maka akan dibutuhkan beberapa waktu. Sehingga, jika tidak menggunakan ASyncTask, maka akan menyebabkan nge-frezee-nya tampilan/halaman (tidak dapat digunakan). Maka dibutuhkan sebuah background service untuk menangani masalah ini.
Pada contoh berikut, digunakan sebuah class downloadstring.java, sehingga jika ada class lain yang akan men-download string dari URL lain, tinggal panggil.
Pada contoh berikut, digunakan sebuah class downloadstring.java, sehingga jika ada class lain yang akan men-download string dari URL lain, tinggal panggil.
public class DownloadString extends AsyncTask<String, Integer, String> {
public DownloadString()
{
}
public interface DownloadProperties {
void OnStart();
void OnProgress(int progress);
void OnCompleted(String result);
}
private DownloadProperties _properties;
public void setListener(DownloadProperties listener) {
_properties = listener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (_properties != null)
_properties.OnStart();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress(0);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(params[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (_properties != null)
_properties.OnProgress(values[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (_properties != null)
_properties.OnCompleted(result);
}
}
public DownloadString()
{
}
public interface DownloadProperties {
void OnStart();
void OnProgress(int progress);
void OnCompleted(String result);
}
private DownloadProperties _properties;
public void setListener(DownloadProperties listener) {
_properties = listener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (_properties != null)
_properties.OnStart();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress(0);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(params[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (_properties != null)
_properties.OnProgress(values[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (_properties != null)
_properties.OnCompleted(result);
}
}
Untuk penggunaannya :
DownloadString connection = new DownloadString();
connection.setListener(new DownloadProperties() {
@Override
public void OnStart() {
// TODO Auto-generated method stub
}
@Override
public void OnProgress(int progress) {
// TODO Auto-generated method stub
}
@Override
public void OnCompleted(String results) {
connection.setListener(new DownloadProperties() {
@Override
public void OnStart() {
// TODO Auto-generated method stub
}
@Override
public void OnProgress(int progress) {
// TODO Auto-generated method stub
}
@Override
public void OnCompleted(String results) {
// TODO Auto-generated method stub
}
tv.setText(result);
}
});
connection.execute("http://www.google.com");
}
tv.setText(result);
}
});
connection.execute("http://www.google.com");
Tidak ada komentar:
Posting Komentar