Last active
August 29, 2015 14:19
-
-
Save silmood/833e161a103772a1ecaa to your computer and use it in GitHub Desktop.
AsyncTask model for get request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.os.AsyncTask; | |
| import android.util.Log; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import java.util.ArrayList; | |
| public class AsyncTaskRequest extends AsyncTask<String, Void, Object> { | |
| public static final String LOG_TAG = AsyncTaskRequest.class.getSimpleName(); | |
| private AsyncResponse responseListener; | |
| public AsyncTaskRequest(@NonNull AsyncResponse responseListener) { | |
| this.responseListener = responseListener; | |
| } | |
| @Override | |
| protected Object doInBackground(String... params) { | |
| if (params.length == 0) { | |
| responseListener.onError(); | |
| return null; | |
| } | |
| HttpURLConnection urlConnection = null; | |
| BufferedReader reader = null; | |
| String location = params[0]; | |
| String response; | |
| try { | |
| URL url = new URL(); //ToDo: Put your url here | |
| urlConnection = (HttpURLConnection) url.openConnection(); | |
| urlConnection.setRequestMethod("GET"); | |
| urlConnection.connect(); | |
| InputStream inputStream = urlConnection.getInputStream(); | |
| StringBuilder buffer = new StringBuilder(); | |
| if (inputStream == null) { | |
| responseListener.onError(); | |
| return null; | |
| } | |
| reader = new BufferedReader(new InputStreamReader(inputStream)); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| buffer.append(line) | |
| .append("\n"); | |
| } | |
| if (buffer.length() == 0) { | |
| responseListener.onError(); | |
| return null; | |
| } | |
| response = buffer.toString(); | |
| }catch (IOException e){ | |
| responseListener.onError(); | |
| return null; | |
| }finally { | |
| if (urlConnection != null) { | |
| urlConnection.disconnect(); | |
| } | |
| if (reader != null) { | |
| try { | |
| reader.close(); | |
| } catch (final IOException e) { | |
| responseListener.onError(); | |
| Log.e(LOG_TAG, "Error closing stream", e); | |
| } | |
| } | |
| } | |
| try{ | |
| return parseResponse(response); //ToDo: Define your parse method | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| return null; | |
| } | |
| @Override | |
| protected void onPostExecute(Object responseObject) { | |
| super.onPostExecute(responseObject); | |
| responseListener.onResponse(responseObject); | |
| } | |
| public interface AsyncResponse { | |
| public void onResponse(Object responseObject); | |
| public void onError(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment