A method to download json data from url - Android File Input Output

Android examples for File Input Output:Json

Description

A method to download json data from url

Demo Code


//package com.java2s;
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 android.util.Log;

public class Main {
    private static final String TAG = "HttpUtils";

    /** A method to download json data from url */
    public static String downloadJson(String strUrl) throws IOException {
        Log.i(TAG, "downloading JSON data of the nearest MRT station");

        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try {/*w  w  w  .  j  a  v a 2 s  .c om*/
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    iStream));

            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        } catch (Exception e) {
            Log.e(TAG, "problem downloading JSON from Google", e);
        } finally {
            iStream.close();
            urlConnection.disconnect();
        }

        return data;
    }
}

Related Tutorials