Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**
     * Send the Http's request with the address of url
     * @param String url
     * @return String
     */
    public static String getHttpRequest(String url) {
        //should use the StringBuilder or StringBuffer, String is not used.
        StringBuffer jsonContent = new StringBuffer();
        try {
            URL getUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
            connection.connect();
            //Getting the inputting stream, and then read the stream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines = "";
            while ((lines = reader.readLine()) != null) {
                jsonContent.append(lines);
            }
            reader.close();
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //ScanningActivity.log("getHttpRequest(): " + content);
        //BooksPutIn.log("getHttpRequest(): " + content);
        return jsonContent.toString();
    }
}