get Json String From Url - Android File Input Output

Android examples for File Input Output:Json String

Description

get Json String From Url

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.StrictMode;

public class Main {
    public static String getJsonStringFromUrl(String urlString) {
        String dateString = null;
        try {//from   w ww  .  ja  v  a2 s.  com

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);

            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();

            conn.setRequestMethod("GET");
            //InputStream in = conn.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            final StringBuffer stringBuffer = new StringBuffer();
            String str;
            while ((str = reader.readLine()) != null) {
                stringBuffer.append(str);
            }

            dateString = stringBuffer.toString();
            System.out.println("=========");
            return dateString;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out
                    .println("********************jsonutil.getJsonStringFromUrl->");
        }
        return dateString;
    }
}

Related Tutorials