org.json.JSONUrl.java Source code

Java tutorial

Introduction

Here is the source code for org.json.JSONUrl.java

Source

package org.json;

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

/*
 *  This file is part of Jotta.
 *
 *  Jotta is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jotta is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Jotta.  If not, see <http://www.gnu.org/licenses/>.
 */

public class JSONUrl {

    public static JSONObject getStringFromURL(URL u) {
        try {
            BufferedReader r = new BufferedReader(new InputStreamReader(u.openConnection().getInputStream()));
            String json = "", line = null;
            while ((line = r.readLine()) != null) {
                json += line.replaceAll("geoPlugin\\(", "");
            }
            return new JSONObject(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}