Example usage for org.jsoup.helper HttpConnection connect

List of usage examples for org.jsoup.helper HttpConnection connect

Introduction

In this page you can find the example usage for org.jsoup.helper HttpConnection connect.

Prototype

public static Connection connect(URL url) 

Source Link

Usage

From source file:br.ufrgs.ufrgsmapas.network.LocationParser.java

public static void parseBuildings(SparseArray<BuildingVo> mTempBuilding) {

    // Read JSON File
    Connection con = HttpConnection.connect(URL);
    con.method(Connection.Method.POST).ignoreContentType(true);
    Connection.Response resp;
    try {//w w w  . j  a  v  a 2s.c o  m
        resp = con.execute();
    } catch (IOException e) {
        if (DebugUtils.DEBUG)
            Log.e(TAG, "Error fetching positions: " + e);
        return;
    }
    String jsonDoc = resp.body();

    // Parse JSON to find each element
    JSONObject jsonStartObject = (JSONObject) JSONValue.parse(jsonDoc);

    // Get the array with objects representing each building
    JSONArray jsonBuildings = (JSONArray) jsonStartObject.get("features");

    int buildingsListSize = ((List) jsonBuildings).size();
    // Iterate through buildings
    for (int i = 0; i < buildingsListSize; i++) {
        // Get buildingVo object
        JSONObject jsonBuilding = (JSONObject) jsonBuildings.get(i);

        // Get buildingVo id
        int id = ((Number) jsonBuilding.get("id")).intValue();
        // Get coordinates array
        JSONObject jsonGeometry = (JSONObject) jsonBuilding.get("geometry");
        JSONArray jsonCoordinates = (JSONArray) ((JSONArray) ((JSONArray) jsonGeometry.get("coordinates"))
                .get(0)).get(0);

        // Get latitudes and longitudes
        List coordList = jsonCoordinates;
        int coordListSize = coordList.size();
        double[] latitude = new double[coordListSize];
        double[] longitude = new double[coordListSize];
        for (int j = 0; j < coordListSize; j++) {
            latitude[j] = (double) ((List) coordList.get(j)).get(1);
            longitude[j] = (double) ((List) coordList.get(j)).get(0);
        }

        // Create buildingVo object and add to list - store only the center of the position
        double[] center = centroid(latitude, longitude);
        BuildingVo buildingVo = new BuildingVo(id, center[0], center[1]);
        mTempBuilding.append(id, buildingVo);

    }

}

From source file:br.ufrgs.ufrgsmapas.network.ExtraInfoParser.java

/**
 * Get the information for a buildingVo/*w w w.  j  ava  2s  .c  om*/
 * @param buildingVo The incomplete BuildingVo object. The extra information will be added to
 *                 this object.
 * @return The same BuildingVo object.
 */
private static BuildingVo fillBuildingInformation(BuildingVo buildingVo) {

    int buildingId = buildingVo.id;
    String URL = BASE_URL + buildingId;

    // Read JSON File
    Connection con = HttpConnection.connect(URL);
    con.method(Connection.Method.POST).ignoreContentType(true);
    Connection.Response resp;
    try {
        resp = con.execute();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    String jsonDoc = resp.body();

    JSONObject jsonStartObject = (JSONObject) JSONValue.parse(jsonDoc);

    // Create a BuildingVo object
    buildingVo.name = (String) jsonStartObject.get("NomePredio");
    buildingVo.ufrgsBuildingCode = (String) jsonStartObject.get("CodPredioUFRGS");
    buildingVo.buildingAddress = (String) jsonStartObject.get("Logradouro");
    buildingVo.buildingAddressNumber = (String) jsonStartObject.get("NrLogradouro");
    buildingVo.zipCode = (String) jsonStartObject.get("CEP");
    buildingVo.neighborhood = (String) jsonStartObject.get("Bairro");
    buildingVo.city = (String) jsonStartObject.get("Cidade");
    buildingVo.state = (String) jsonStartObject.get("UF");
    buildingVo.campusCode = Integer.valueOf((String) jsonStartObject.get("Campus"));
    buildingVo.isExternalBuilding = (String) jsonStartObject.get("IndicadorPredioExterno");
    buildingVo.description = (String) jsonStartObject.get("Descricao");
    buildingVo.phone = (String) jsonStartObject.get("TelefonePortaria");
    buildingVo.isHistorical = (String) jsonStartObject.get("IndicadorPredioHistorico");
    buildingVo.locationUrl = (String) jsonStartObject.get("URLLocalizacao");

    return buildingVo;

}