Created
August 6, 2014 03:50
-
-
Save johnsonyeap/471738699e74ea7ce5e8 to your computer and use it in GitHub Desktop.
HTTP GET
This file contains 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
package com.runningbus.get; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
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 ActionBarActivity { | |
private GoogleMap mMap; | |
private TextView lat, lng; | |
private ConnectivityManager connMgr; | |
private NetworkInfo networkInfo; | |
private JSONObject jObject; | |
private double latitude, longtitude; | |
private String dataSet; | |
private static final String GPS = "http://protected-brushlands-3658.herokuapp.com/locations.json"; | |
private static final String DEBUG_TAG = "HttpExample"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initializeVariables(); | |
initializeMap(); | |
initializeMapUtils(); | |
mMap.setMyLocationEnabled(true); | |
if(networkInfo != null && networkInfo.isConnected()) | |
new getData().execute(GPS); | |
else | |
Toast.makeText(getApplicationContext(), "Please connect to the Internet", Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
initializeMap(); | |
} | |
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 void initializeVariables() { | |
lat = (TextView) findViewById(R.id.tvLatitude); | |
lng = (TextView) findViewById(R.id.tvLongtitude); | |
connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
networkInfo = connMgr.getActiveNetworkInfo(); | |
} | |
private class getData extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... urls) { | |
try { | |
dataSet = downloadUrl(urls[0]); | |
} catch(IOException e) { | |
return "Unable to retrieve web page. URL may be invalid"; | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
try { | |
jObject = new JSONObject(dataSet); | |
latitude = Double.parseDouble(jObject.getString("lat")); | |
longtitude = Double.parseDouble(jObject.getString("lng")); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longtitude), 16)); | |
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longtitude))); | |
lat.setText("" + latitude); | |
lng.setText("" + longtitude); | |
} | |
} | |
//Fetches and processes the web page content and passes back a result string. | |
private String downloadUrl(String myurl) throws IOException { | |
String contentAsString; | |
InputStream is = null; | |
// Only display the first 500 characters of the retrieved | |
// web page content. | |
int len = 500; | |
try { | |
URL url = new URL(myurl); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setReadTimeout(10000 /* milliseconds */); | |
conn.setConnectTimeout(15000 /* milliseconds */); | |
conn.setRequestMethod("GET"); | |
conn.setDoInput(true); | |
// Starts the query | |
conn.connect(); | |
int response = conn.getResponseCode(); | |
Log.d(DEBUG_TAG, "The GET response is: " + response); | |
is = conn.getInputStream(); | |
// Convert the InputStream into a string | |
contentAsString = readIt(is, len); | |
return contentAsString; | |
// Makes sure that the InputStream is closed after the app is | |
// finished using it. | |
} finally { | |
if (is != null) { | |
is.close(); | |
} | |
} | |
} | |
// Reads an InputStream and converts it to a String. | |
public String readIt(InputStream stream, int len) throws IOException { | |
Reader reader = new InputStreamReader(stream, "UTF-8"); | |
char[] buffer = new char[len]; | |
reader.read(buffer); | |
return new String(buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment