Skip to content

Instantly share code, notes, and snippets.

@johnsonyeap
Created August 6, 2014 01:58
Show Gist options
  • Save johnsonyeap/2da5f6410d33d20ef4a0 to your computer and use it in GitHub Desktop.
Save johnsonyeap/2da5f6410d33d20ef4a0 to your computer and use it in GitHub Desktop.
HTTP in doInBackground of AsyncTask
package com.runningbus.put;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
private TextView lat, lng;
private ConnectivityManager connMgr;
private NetworkInfo networkInfo;
private LocationManager manager;
private LocationListener listener;
private HttpClient httpClient;
private HttpPut httpPut;
private JSONObject pairs;
private double latitude, longitude;
private Location busLocation;
private static final String GPS = "http://protected-brushlands-3658.herokuapp.com/locations/1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();
initializeMap();
initializeMapUtils();
mMap.setMyLocationEnabled(true);
manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())));
lat.setText("" + location.getLatitude());
lng.setText("" + location.getLongitude());
busLocation = location;
new PutData().execute(GPS);
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
manager.removeUpdates(listener);
}
@Override
protected void onResume() {
super.onResume();
initializeMap();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
private void initializeVariables() {
lat = (TextView) findViewById(R.id.tvLatitude);
lng = (TextView) findViewById(R.id.tvLongtitude);
connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connMgr.getActiveNetworkInfo();
pairs = new JSONObject();
}
private void initializeMapUtils() {
// Enable / Disable zooming controls
mMap.getUiSettings().setZoomControlsEnabled(true);
// Enable / Disable Compass icon
mMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
mMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
mMap.getUiSettings().setZoomGesturesEnabled(true);
}
private void initializeMap() {
if (mMap != null)
return;
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
private class PutData extends AsyncTask<String, Void, Void> {
//Location busLocation;
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
httpClient = new DefaultHttpClient();
httpPut = new HttpPut(params[0]);
pairs.put("id", "1");
pairs.put("lat", "" + busLocation.getLatitude());
pairs.put("lng", "" + busLocation.getLongitude());
httpPut.setEntity(new StringEntity(pairs.toString()));
Log.d("JSON", pairs.toString());
HttpResponse response = httpClient.execute(httpPut);
Log.d("CODE", response.getStatusLine().getStatusCode() + "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment