Java tutorial
package com.sanjaydalvi.spacestationlocator; /* * Copyright (C) 2016 Sanjay Dalvi * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.util.Log; public class LoadISSLocation extends AsyncTask<HttpUriRequest, Void, String> { private static final String TAG = "Space Station Locator"; public static final String HTTP_RESPONSE = "httpResponse"; private Context mContext; private HttpClient mClient; private String mAction; public LoadISSLocation(Context context, String action) { mContext = context; mAction = action; mClient = new DefaultHttpClient(); } public LoadISSLocation(Context context, String action, HttpClient client) { mContext = context; mAction = action; mClient = client; } @Override protected String doInBackground(HttpUriRequest... params) { try { HttpUriRequest request = params[0]; HttpResponse serverResponse = mClient.execute(request); BasicResponseHandler handler = new BasicResponseHandler(); return handler.handleResponse(serverResponse); } catch (Exception e) { // TODO handle this properly e.printStackTrace(); return ""; } } @Override protected void onPostExecute(String result) { Log.i(TAG, "RESULT = " + result); Intent intent = new Intent(mAction); intent.putExtra(HTTP_RESPONSE, result); // broadcast the completion mContext.sendBroadcast(intent); } }