Mobile_Application_Developm.../AirKoality/app/src/main/java/at/fhj/airkoality/network/HttpsGetTask.java

60 lines
1.6 KiB
Java

package at.fhj.airkoality.network;
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import java.net.URL;
//Async task has 3 generic types for:
//Params
//Progress
//Result
public class HttpsGetTask extends AsyncTask<String, Void, String> {
private static final String TAG = "HttpsGetTask";
private RequestCallback callback;
public HttpsGetTask(RequestCallback callback) {
this.callback = callback;
}
//Do preparation here
//this is executed on UI thread, so no network communication here!
@Override
protected void onPreExecute() {
Log.d(TAG, "Preparing ...");
if(callback != null) {
callback.onRequestStart();
}
}
//this runs in background
//will be called after onPreExecute
@Override
protected String doInBackground(String... params) {
Log.d(TAG, "Doing background work ...");
HttpsClient httpsClient = new HttpsClient();
String result = null;
try {
result = httpsClient.get(new URL(params[0]));
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//will be called after doInBackground
//runs on UI thread
//gets the result of doInBackground as its parameter
@Override
protected void onPostExecute(String result) {
if(callback != null)
callback.onResult(result);
}
//will be called if publishProgress is called during doInBackground
//runs on UI thread
@Override
protected void onProgressUpdate(Void... values) {
}
}