get Json Content from URL - Android File Input Output

Android examples for File Input Output:Json

Description

get Json Content from URL

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static String getJsonContent(String path) {
        try {/*from w  ww . j  a va 2 s  .co  m*/
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setConnectTimeout(3000);
            connection.setRequestMethod("GET");

            connection.setDoInput(true);

            int code = connection.getResponseCode();
            // Log.i("apkm","Path is " + path); 
            // Log.i("apkm","Code is " + code); 
            if (code == 200) {
                // Log.i("apkm","Connection success."); 
                return changeInputString(connection.getInputStream());
            }
        } catch (Exception e) {
            // TODO: handle exception 
            e.printStackTrace();
        }
        return "";
    }

    private static String changeInputString(InputStream inputStream) {

        String jsonString = "";

        ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        try {
            while ((len = inputStream.read(data)) != -1) {
                outPutStream.write(data, 0, len);
            }
            jsonString = new String(outPutStream.toByteArray());

        } catch (Exception e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
        return jsonString;
    }
}

Related Tutorials