Advertisement
Guest User

JSONHelper.java

a guest
Sep 16th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.66 KB | None | 0 0
  1. public class JSONHelper
  2. {
  3.     private InputStream     is              = null;
  4.     private JSONObject      jsonObject      = null;
  5.     private String          json            = "";
  6.  
  7.     private final String    TAG_BANDARA     = "data_bandara";
  8.     private final String    TAG_PELABUHAN   = "data_pelabuhan";
  9.     private final String    TAG_STASIUN     = "data_stasiun";
  10.     private final String    TAG_TERMINAL    = "data_terminal";
  11.     private final String    TAG_ID          = "id";
  12.     private final String    TAG_NAMA        = "nama";
  13.     private final String    TAG_ALAMAT      = "alamat";
  14.     private final String    TAG_LAT         = "lat";
  15.     private final String    TAG_LNG         = "lng";
  16.     private final String    TAG_ROUTES      = "routes";
  17.     private final String    TAG_LEGS        = "legs";
  18.     private final String    TAG_STEPS       = "steps";
  19.     private final String    TAG_POLYLINE    = "polyline";
  20.     private final String    TAG_POINTS      = "points";
  21.     private final String    TAG_START       = "start_location";
  22.     private final String    TAG_END         = "end_location";
  23.  
  24.     public JSONObject getJSONFromURL(String url)
  25.     {
  26.         try
  27.         {
  28.             DefaultHttpClient httpClient = new DefaultHttpClient();
  29.             HttpGet httpGet = new HttpGet(url);
  30.  
  31.             HttpResponse httpResponse = httpClient.execute(httpGet);
  32.             HttpEntity httpEntity = httpResponse.getEntity();
  33.             is = httpEntity.getContent();
  34.         } catch (UnsupportedEncodingException e)
  35.         {
  36.             e.printStackTrace();
  37.         } catch (ClientProtocolException e)
  38.         {
  39.             e.printStackTrace();
  40.         } catch (IOException e)
  41.         {
  42.             e.printStackTrace();
  43.         }
  44.  
  45.         try
  46.         {
  47.             BufferedReader reader = new BufferedReader(new InputStreamReader(
  48.                     is, "iso-8859-1"), 8);
  49.  
  50.             StringBuilder sb = new StringBuilder();
  51.             String line = null;
  52.  
  53.             while ((line = reader.readLine()) != null)
  54.             {
  55.                 sb.append(line + "\n");
  56.             }
  57.  
  58.             is.close();
  59.             json = sb.toString();
  60.         } catch (Exception e)
  61.         {
  62.             // TODO: handle exception
  63.         }
  64.  
  65.         try
  66.         {
  67.             jsonObject = new JSONObject(json);
  68.  
  69.         } catch (JSONException e)
  70.         {
  71.             // TODO: handle exception
  72.         }
  73.  
  74.         return jsonObject;
  75.     }
  76.  
  77.     public ArrayList<Map_POI> getPOI_BandaraAll(JSONObject jobj)
  78.     {
  79.         ArrayList<Map_POI> listPOI_Bandara = new ArrayList<Map_POI>();
  80.  
  81.         try
  82.         {
  83.             JSONArray arrayPOI_Bandara = jobj.getJSONArray(TAG_BANDARA);
  84.  
  85.             for (int i = 0; i < arrayPOI_Bandara.length(); i++)
  86.             {
  87.                 JSONObject jobject = arrayPOI_Bandara.getJSONObject(i);
  88.  
  89.                 Log.d("log", "muter ke " + i);
  90.                 listPOI_Bandara.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
  91.                         .getString(TAG_ALAMAT), jobject
  92.                         .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
  93.  
  94.             }
  95.         } catch (JSONException e)
  96.         {
  97.             e.printStackTrace();
  98.         }
  99.         return listPOI_Bandara;
  100.     }
  101.  
  102.     public ArrayList<Map_POI> getPOI_PelabuhanAll(JSONObject jobj)
  103.     {
  104.         ArrayList<Map_POI> listPOI_Pelabuhan = new ArrayList<Map_POI>();
  105.  
  106.         try
  107.         {
  108.             JSONArray arrayPOI_Pelabuhan = jobj.getJSONArray(TAG_PELABUHAN);
  109.  
  110.             for (int i = 0; i < arrayPOI_Pelabuhan.length(); i++)
  111.             {
  112.                 JSONObject jobject = arrayPOI_Pelabuhan.getJSONObject(i);
  113.  
  114.                 Log.d("log", "muter ke " + i);
  115.                 listPOI_Pelabuhan.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
  116.                         .getString(TAG_ALAMAT), jobject
  117.                         .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
  118.  
  119.             }
  120.         } catch (JSONException e)
  121.         {
  122.             e.printStackTrace();
  123.         }
  124.         return listPOI_Pelabuhan;
  125.     }
  126.  
  127.     public ArrayList<Map_POI> getPOI_StasiunAll(JSONObject jobj)
  128.     {
  129.         ArrayList<Map_POI> listPOI_Stasiun = new ArrayList<Map_POI>();
  130.  
  131.         try
  132.         {
  133.             JSONArray arrayPOI_Stasiun = jobj.getJSONArray(TAG_STASIUN);
  134.  
  135.             for (int i = 0; i < arrayPOI_Stasiun.length(); i++)
  136.             {
  137.                 JSONObject jobject = arrayPOI_Stasiun.getJSONObject(i);
  138.  
  139.                 Log.d("log", "muter ke " + i);
  140.                 listPOI_Stasiun.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
  141.                         .getString(TAG_ALAMAT), jobject
  142.                         .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
  143.  
  144.             }
  145.         } catch (JSONException e)
  146.         {
  147.             e.printStackTrace();
  148.         }
  149.         return listPOI_Stasiun;
  150.     }
  151.  
  152.     public ArrayList<Map_POI> getPOI_TerminalAll(JSONObject jobj)
  153.     {
  154.         ArrayList<Map_POI> listPOI_Terminal = new ArrayList<Map_POI>();
  155.  
  156.         try
  157.         {
  158.             JSONArray arrayPOI_Terminal = jobj.getJSONArray(TAG_TERMINAL);
  159.  
  160.             for (int i = 0; i < arrayPOI_Terminal.length(); i++)
  161.             {
  162.                 JSONObject jobject = arrayPOI_Terminal.getJSONObject(i);
  163.  
  164.                 Log.d("log", "muter ke " + i);
  165.                 listPOI_Terminal.add(new Map_POI( jobject.getInt(TAG_ID), jobject.getString(TAG_NAMA), jobject
  166.                         .getString(TAG_ALAMAT), jobject
  167.                         .getDouble(TAG_LAT), jobject.getDouble(TAG_LNG)));
  168.  
  169.             }
  170.         } catch (JSONException e)
  171.         {
  172.             e.printStackTrace();
  173.         }
  174.         return listPOI_Terminal;
  175.     }
  176.  
  177.     /*
  178.      * Untuk decode Polyline
  179.      *
  180.      * @params String
  181.      *
  182.      * @return List<LatLng>
  183.      */
  184.     private List<LatLng> decodePoly(String encoded)
  185.     {
  186.         List<LatLng> poly = new ArrayList<LatLng>();
  187.         int index = 0, len = encoded.length();
  188.         int lat = 0, lng = 0;
  189.         while (index < len)
  190.         {
  191.             int b, shift = 0, result = 0;
  192.             do
  193.             {
  194.                 b = encoded.charAt(index++) - 63;
  195.                 result |= (b & 0x1f) << shift;
  196.                 shift += 5;
  197.             } while (b >= 0x20);
  198.             int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  199.             lat += dlat;
  200.             shift = 0;
  201.             result = 0;
  202.             do
  203.             {
  204.                 b = encoded.charAt(index++) - 63;
  205.                 result |= (b & 0x1f) << shift;
  206.                 shift += 5;
  207.             } while (b >= 0x20);
  208.             int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  209.             lng += dlng;
  210.  
  211.             LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
  212.             poly.add(position);
  213.         }
  214.         return poly;
  215.  
  216.     }
  217.  
  218.     /*
  219.      * Untuk mendapatkan direction
  220.      *
  221.      * @params JSONObject
  222.      *
  223.      * @return List<LatLng>
  224.      */
  225.     public List<LatLng> getDirection(JSONObject jObj)
  226.     {
  227.  
  228.         List<LatLng> directions = new ArrayList<LatLng>();
  229.  
  230.         try
  231.         {
  232.             JSONObject objRoute = jObj.getJSONArray(TAG_ROUTES).getJSONObject(0);
  233.             JSONObject objLegs = objRoute.getJSONArray(TAG_LEGS).getJSONObject(0);
  234.             JSONArray arraySteps = objLegs.getJSONArray(TAG_STEPS);
  235.             for (int wi2t = 0; wi2t < arraySteps.length(); wi2t++)
  236.             {
  237.                 JSONObject step = arraySteps.getJSONObject(wi2t);
  238.                 JSONObject objStart = step.getJSONObject(TAG_START);
  239.                 JSONObject objEnd = step.getJSONObject(TAG_END);
  240.                 double latStart = objStart.getDouble(TAG_LAT);
  241.                 double lngStart = objStart.getDouble(TAG_LNG);
  242.  
  243.                 directions.add(new LatLng(latStart, lngStart));
  244.  
  245.                 JSONObject poly = step.getJSONObject(TAG_POLYLINE);
  246.                 String encodedPoly = poly.getString(TAG_POINTS);
  247.  
  248.                 List<LatLng> decodedPoly = decodePoly(encodedPoly);
  249.                 for (int eka = 0; eka < decodedPoly.size(); eka++)
  250.                 {
  251.                     directions.add(new LatLng(decodedPoly.get(eka).latitude, decodedPoly.get(eka).longitude));
  252.                 }
  253.  
  254.                 double latEnd = objEnd.getDouble(TAG_LAT);
  255.                 double lngEnd = objEnd.getDouble(TAG_LNG);
  256.                 directions.add(new LatLng(latEnd, lngEnd));
  257.  
  258.             }
  259.         } catch (JSONException e)
  260.         {
  261.             // TODO: handle exception
  262.         }
  263.  
  264.         return directions;
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement