Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JSONHelper
- {
- private InputStream is = null;
- private JSONObject jsonObject = null;
- private String json = "";
- private final String TAG_BANDARA = "data_bandara";
- private final String TAG_PELABUHAN = "data_pelabuhan";
- private final String TAG_STASIUN = "data_stasiun";
- private final String TAG_TERMINAL = "data_terminal";
- private final String TAG_ID = "id";
- private final String TAG_NAMA = "nama";
- private final String TAG_ALAMAT = "alamat";
- private final String TAG_LAT = "lat";
- private final String TAG_LNG = "lng";
- private final String TAG_ROUTES = "routes";
- private final String TAG_LEGS = "legs";
- private final String TAG_STEPS = "steps";
- private final String TAG_POLYLINE = "polyline";
- private final String TAG_POINTS = "points";
- private final String TAG_START = "start_location";
- private final String TAG_END = "end_location";
- public JSONObject getJSONFromURL(String url)
- {
- try
- {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url);
- HttpResponse httpResponse = httpClient.execute(httpGet);
- HttpEntity httpEntity = httpResponse.getEntity();
- is = httpEntity.getContent();
- } catch (UnsupportedEncodingException e)
- {
- e.printStackTrace();
- } catch (ClientProtocolException e)
- {
- e.printStackTrace();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- try
- {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- is, "iso-8859-1"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- is.close();
- json = sb.toString();
- } catch (Exception e)
- {
- // TODO: handle exception
- }
- try
- {
- jsonObject = new JSONObject(json);
- } catch (JSONException e)
- {
- // TODO: handle exception
- }
- return jsonObject;
- }
- public ArrayList<Map_POI> getPOI_BandaraAll(JSONObject jobj)
- {
- ArrayList<Map_POI> listPOI_Bandara = new ArrayList<Map_POI>();
- try
- {
- JSONArray arrayPOI_Bandara = jobj.getJSONArray(TAG_BANDARA);
- for (int i = 0; i < arrayPOI_Bandara.length(); i++)
- {
- JSONObject jobject = arrayPOI_Bandara.getJSONObject(i);
- Log.d("log", "muter ke " + i);
- listPOI_Bandara.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
- .getString(TAG_ALAMAT), jobject
- .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
- }
- } catch (JSONException e)
- {
- e.printStackTrace();
- }
- return listPOI_Bandara;
- }
- public ArrayList<Map_POI> getPOI_PelabuhanAll(JSONObject jobj)
- {
- ArrayList<Map_POI> listPOI_Pelabuhan = new ArrayList<Map_POI>();
- try
- {
- JSONArray arrayPOI_Pelabuhan = jobj.getJSONArray(TAG_PELABUHAN);
- for (int i = 0; i < arrayPOI_Pelabuhan.length(); i++)
- {
- JSONObject jobject = arrayPOI_Pelabuhan.getJSONObject(i);
- Log.d("log", "muter ke " + i);
- listPOI_Pelabuhan.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
- .getString(TAG_ALAMAT), jobject
- .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
- }
- } catch (JSONException e)
- {
- e.printStackTrace();
- }
- return listPOI_Pelabuhan;
- }
- public ArrayList<Map_POI> getPOI_StasiunAll(JSONObject jobj)
- {
- ArrayList<Map_POI> listPOI_Stasiun = new ArrayList<Map_POI>();
- try
- {
- JSONArray arrayPOI_Stasiun = jobj.getJSONArray(TAG_STASIUN);
- for (int i = 0; i < arrayPOI_Stasiun.length(); i++)
- {
- JSONObject jobject = arrayPOI_Stasiun.getJSONObject(i);
- Log.d("log", "muter ke " + i);
- listPOI_Stasiun.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
- .getString(TAG_ALAMAT), jobject
- .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
- }
- } catch (JSONException e)
- {
- e.printStackTrace();
- }
- return listPOI_Stasiun;
- }
- public ArrayList<Map_POI> getPOI_TerminalAll(JSONObject jobj)
- {
- ArrayList<Map_POI> listPOI_Terminal = new ArrayList<Map_POI>();
- try
- {
- JSONArray arrayPOI_Terminal = jobj.getJSONArray(TAG_TERMINAL);
- for (int i = 0; i < arrayPOI_Terminal.length(); i++)
- {
- JSONObject jobject = arrayPOI_Terminal.getJSONObject(i);
- Log.d("log", "muter ke " + i);
- listPOI_Terminal.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
- .getString(TAG_ALAMAT), jobject
- .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
- }
- } catch (JSONException e)
- {
- e.printStackTrace();
- }
- return listPOI_Terminal;
- }
- /*
- * Untuk decode Polyline
- *
- * @params String
- *
- * @return List<LatLng>
- */
- private List<LatLng> decodePoly(String encoded)
- {
- List<LatLng> poly = new ArrayList<LatLng>();
- int index = 0, len = encoded.length();
- int lat = 0, lng = 0;
- while (index < len)
- {
- int b, shift = 0, result = 0;
- do
- {
- b = encoded.charAt(index++) - 63;
- result |= (b & 0x1f) << shift;
- shift += 5;
- } while (b >= 0x20);
- int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
- lat += dlat;
- shift = 0;
- result = 0;
- do
- {
- b = encoded.charAt(index++) - 63;
- result |= (b & 0x1f) << shift;
- shift += 5;
- } while (b >= 0x20);
- int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
- lng += dlng;
- LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
- poly.add(position);
- }
- return poly;
- }
- /*
- * Untuk mendapatkan direction
- *
- * @params JSONObject
- *
- * @return List<LatLng>
- */
- public List<LatLng> getDirection(JSONObject jObj)
- {
- List<LatLng> directions = new ArrayList<LatLng>();
- try
- {
- JSONObject objRoute = jObj.getJSONArray(TAG_ROUTES).getJSONObject(0);
- JSONObject objLegs = objRoute.getJSONArray(TAG_LEGS).getJSONObject(0);
- JSONArray arraySteps = objLegs.getJSONArray(TAG_STEPS);
- for (int wi2t = 0; wi2t < arraySteps.length(); wi2t++)
- {
- JSONObject step = arraySteps.getJSONObject(wi2t);
- JSONObject objStart = step.getJSONObject(TAG_START);
- JSONObject objEnd = step.getJSONObject(TAG_END);
- double latStart = objStart.getDouble(TAG_LAT);
- double lngStart = objStart.getDouble(TAG_LNG);
- directions.add(new LatLng(latStart, lngStart));
- JSONObject poly = step.getJSONObject(TAG_POLYLINE);
- String encodedPoly = poly.getString(TAG_POINTS);
- List<LatLng> decodedPoly = decodePoly(encodedPoly);
- for (int eka = 0; eka < decodedPoly.size(); eka++)
- {
- directions.add(new LatLng(decodedPoly.get(eka).latitude, decodedPoly.get(eka).longitude));
- }
- double latEnd = objEnd.getDouble(TAG_LAT);
- double lngEnd = objEnd.getDouble(TAG_LNG);
- directions.add(new LatLng(latEnd, lngEnd));
- }
- } catch (JSONException e)
- {
- // TODO: handle exception
- }
- return directions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement