[Android] Fetch Data using URL Link (JSON/XML)

As usual, before getting into this tutorial, you need to know how an android application code works and how different important file interact with each other (To be updated)

1. Insert the following into your Manifest.xml (AndroidManifest.xml, understand the tab) to enable the permissions to access internet

 <!-- Adding for accessing internet -->
 <uses-permission android:name="android.permission.INTERNET" />

2. Create the following method to read Data from internet

/**
     * Get Data, based on url input
     * @param urlPath path linked to the data
     * @return null if not data is found, or String if there is data
     */

    public static String readURL(String urlPath) {
        // These two need to be declared outside the try/catch
        // so that they can be closed in the finally block.
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        // Will contain the raw text response as a string.
        String textStr = null;
        try {
            // Construct the URL based on the link provided
            URL url = new URL(urlPath);

            // Create the request to the link provided
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if (inputStream == null) {
                // Nothing to do.
                textStr = null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;

            // Reading the strings line by line and append new line (\n)
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty. No point in parsing.
                textStr = null;
            }
            textStr = buffer.toString();
        } catch (IOException e) {
            Log.e("PlaceholderFragment", "Error ", e);
            // If the code didn't successfully get the string, there's no point in attempting
            // to parse it.
            textStr = null;
        } finally{
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("PlaceholderFragment", "Error closing stream", e);
                }
            }
        }

        return textStr;
    }

3. Then you can call the above method, which will attempt to read the data based on the link that you have provided! Android development recommends the usage of Thread to retrieve data so that the operation of application is not interrupted by the data retrieve process. (To be updated)

4. That’s all! Try to code it now!

Reference:
1. Connecting to the Network, Android Development: http://developer.android.com/training/basics/network-ops/connecting.html
2. Data retrieve methods from github: https://github.com/stilva/udacity-dev-android/blob/master/app/src/main/java/com/stilva/android/sunshine/app/FetchWeatherTask.java

[Android] Get your GPS Coordinates

Before getting into this tutorial, you need to know how an android application code works and how different important file interact with each other (To be updated)

1. Insert the following code into your Manifest.xml to enable accessing location services:

<!-- Adding for accessing location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

2. Add the following code to onCreate() in your Activity.java


// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new myLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0 , locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0 , locationListener);

3. Lastly, create a private class inside your Activity class


private class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {


if(location != null) {
Toast.makeText(LocationService.this,
location.getLatitude() + "\n" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
else {
}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {  }

@Override
public void onProviderEnabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText( getApplicationContext(),"Gps Enabled", Toast.LENGTH_SHORT ).show();
}
}

 

There you go! Try the code now!