Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.util.Log;
import java.io.BufferedReader;
import java.io.Closeable;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

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

    public static String getJsonWithPath(String path) {
        StringBuffer sb = new StringBuffer();
        URL url = null;
        HttpURLConnection conn = null;
        BufferedReader br = null;

        try {
            url = new URL(path);
            conn = (HttpURLConnection) url.openConnection();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String temp = "";
                while ((temp = br.readLine()) != null) {
                    sb.append(temp);
                }
            } else {
                Log.e(TAG, " NET IS ERROR");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(br);
            conn.disconnect();
        }
        Log.i("TAG", "---" + sb.toString());
        return sb.toString();
    }

    public static void close(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}