Search in sources :

Example 76 with JSONException

use of org.json.JSONException in project android-maps-utils by googlemaps.

the class GeoJsonParser method parseFeatureCollection.

/**
     * Parses the array of GeoJSON features in a given GeoJSON feature collection. Also parses the
     * bounding box member of the feature collection if it exists.
     *
     * @param geoJsonFeatureCollection feature collection to parse
     * @return array of GeoJsonFeature objects
     */
private ArrayList<GeoJsonFeature> parseFeatureCollection(JSONObject geoJsonFeatureCollection) {
    JSONArray geoJsonFeatures;
    ArrayList<GeoJsonFeature> features = new ArrayList<GeoJsonFeature>();
    try {
        geoJsonFeatures = geoJsonFeatureCollection.getJSONArray(FEATURE_COLLECTION_ARRAY);
        if (geoJsonFeatureCollection.has(BOUNDING_BOX)) {
            mBoundingBox = parseBoundingBox(geoJsonFeatureCollection.getJSONArray(BOUNDING_BOX));
        }
    } catch (JSONException e) {
        Log.w(LOG_TAG, "Feature Collection could not be created.");
        return features;
    }
    for (int i = 0; i < geoJsonFeatures.length(); i++) {
        try {
            JSONObject feature = geoJsonFeatures.getJSONObject(i);
            if (feature.getString("type").equals(FEATURE)) {
                GeoJsonFeature parsedFeature = parseFeature(feature);
                if (parsedFeature != null) {
                    // Don't add null features
                    features.add(parsedFeature);
                } else {
                    Log.w(LOG_TAG, "Index of Feature in Feature Collection that could not be created: " + i);
                }
            }
        } catch (JSONException e) {
            Log.w(LOG_TAG, "Index of Feature in Feature Collection that could not be created: " + i);
        }
    }
    return features;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 77 with JSONException

use of org.json.JSONException in project OkVolley by googolmo.

the class BaseRequest method parseNetworkResponse.

@Override
protected //解析返回的数据
Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        byte[] data = response.data;
        String json = new String(data, HttpHeaderParser.parseCharset(response.headers));
        if (VolleyLog.DEBUG) {
            VolleyLog.d("response:%s", json);
        }
        return Response.success(new JSONObject(json), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException e) {
        return Response.error(new ParseError(e));
    }
}
Also used : JSONObject(org.json.JSONObject) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) ParseError(com.android.volley.ParseError)

Example 78 with JSONException

use of org.json.JSONException in project android-maps-utils by googlemaps.

the class GeoJsonDemoActivity method retrieveFileFromResource.

private void retrieveFileFromResource() {
    try {
        GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.earthquakes_with_usa, this);
        addGeoJsonLayerToMap(layer);
    } catch (IOException e) {
        Log.e(mLogTag, "GeoJSON file could not be read");
    } catch (JSONException e) {
        Log.e(mLogTag, "GeoJSON file could not be converted to a JSONObject");
    }
}
Also used : GeoJsonLayer(com.google.maps.android.data.geojson.GeoJsonLayer) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 79 with JSONException

use of org.json.JSONException in project android-maps-utils by googlemaps.

the class HeatmapsDemoActivity method startDemo.

@Override
protected void startDemo() {
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-25, 143), 4));
    // Set up the spinner/dropdown list
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.heatmaps_datasets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new SpinnerActivity());
    try {
        mLists.put(getString(R.string.police_stations), new DataSet(readItems(R.raw.police), getString(R.string.police_stations_url)));
        mLists.put(getString(R.string.medicare), new DataSet(readItems(R.raw.medicare), getString(R.string.medicare_url)));
    } catch (JSONException e) {
        Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
    }
// Make the handler deal with the map
// Input: list of WeightedLatLngs, minimum and maximum zoom levels to calculate custom
// intensity from, and the map to draw the heatmap on
// radius, gradient and opacity not specified, so default are used
}
Also used : Spinner(android.widget.Spinner) JSONException(org.json.JSONException) LatLng(com.google.android.gms.maps.model.LatLng)

Example 80 with JSONException

use of org.json.JSONException in project smooth-app-bar-layout by henrytao-me.

the class BaseActivity method requestItemsForPurchase.

private void requestItemsForPurchase(final AsyncCallback<List<PurchaseItem>> callback) {
    if (mPurchaseItems != null && mPurchaseItems.size() > 0) {
        if (callback != null) {
            callback.onSuccess(mPurchaseItems);
        }
        return;
    }
    new AsyncTask<IInAppBillingService, Void, AsyncResult<List<PurchaseItem>>>() {

        @Override
        protected AsyncResult<List<PurchaseItem>> doInBackground(IInAppBillingService... params) {
            List<PurchaseItem> result = new ArrayList<>();
            Throwable exception = null;
            IInAppBillingService billingService = params[0];
            if (billingService == null) {
                exception = new Exception("Unknow");
            } else {
                ArrayList<String> skuList = new ArrayList<>(Arrays.asList(DONATE_ITEMS));
                Bundle querySkus = new Bundle();
                querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
                try {
                    Bundle skuDetails = billingService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
                    int response = skuDetails.getInt("RESPONSE_CODE");
                    if (response == 0) {
                        ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
                        PurchaseItem purchaseItem;
                        for (String item : responseList) {
                            purchaseItem = new PurchaseItem(new JSONObject(item));
                            if (purchaseItem.isValid()) {
                                result.add(purchaseItem);
                            }
                        }
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    exception = e;
                } catch (JSONException e) {
                    e.printStackTrace();
                    exception = e;
                }
            }
            return new AsyncResult<>(result, exception);
        }

        @Override
        protected void onPostExecute(AsyncResult<List<PurchaseItem>> result) {
            if (!isFinishing() && callback != null) {
                Throwable error = result.getError();
                if (error == null && (result.getResult() == null || result.getResult().size() == 0)) {
                    error = new Exception("Unknow");
                }
                if (error != null) {
                    callback.onError(error);
                } else {
                    mPurchaseItems = result.getResult();
                    Collections.sort(mPurchaseItems, new Comparator<PurchaseItem>() {

                        @Override
                        public int compare(PurchaseItem lhs, PurchaseItem rhs) {
                            return (int) ((lhs.getPriceAmountMicros() - rhs.getPriceAmountMicros()) / 1000);
                        }
                    });
                    callback.onSuccess(mPurchaseItems);
                }
            }
        }
    }.execute(mBillingService);
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) RemoteException(android.os.RemoteException) JSONException(org.json.JSONException) Comparator(java.util.Comparator) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) IInAppBillingService(com.android.vending.billing.IInAppBillingService) RemoteException(android.os.RemoteException)

Aggregations

JSONException (org.json.JSONException)1904 JSONObject (org.json.JSONObject)1422 JSONArray (org.json.JSONArray)638 IOException (java.io.IOException)338 ArrayList (java.util.ArrayList)211 HashMap (java.util.HashMap)145 Test (org.junit.Test)128 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 File (java.io.File)77 Bundle (android.os.Bundle)75 VolleyError (com.android.volley.VolleyError)67 Intent (android.content.Intent)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)59 URL (java.net.URL)57 Map (java.util.Map)48 TextView (android.widget.TextView)46 View (android.view.View)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)44