Android examples for Network:URL
A method to download json data from url
//package com.java2s; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Main { private static final String LOG_TAG = "DownloadHelper"; /** A method to download json data from url */ public static String downloadUrl(String strUrl) throws IOException { Log.v(LOG_TAG, "Downloading url: " + strUrl); String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try {/*from ww w. ja v a 2s .com*/ 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.d("Exception while downloading url: " + strUrl, e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; } }