Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

public class Main {
    /**
     * fetch JSONArray from given url, must be called on worker thread
     * @param url
     * @return
     * @throws JSONException
     * @throws IOException
     */
    public static JSONArray getRemoteJsonArray(String url) throws JSONException, IOException {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        HttpResponse response = client.execute(get);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuilder builder = new StringBuilder();
        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            builder.append(s);
        }
        return new JSONArray(builder.toString());
    }
}