Bonjour
je souhaite parser un fichier JSON qui est le résultat de ma requête sur google map afin de récupérer la durée du trajet.
Voici le résultat de ma requête
et voici la mèthode que j'ai implémenté
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 { "destination_addresses" : [ "Laval, QC H7H 2M3, Canada" ], "origin_addresses" : [ "Laval, QC H7H 1Y3, Canada" ], "rows" : [ { "elements" : [ { "distance" : { "text" : "2,8 km", "value" : 2848 }, "duration" : { "text" : "6 minutes", "value" : 332 }, "status" : "OK" } ] } ], "status" : "OK" }
Merci pour votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 private void drawPath(String scr, String dest, MapView mapView){ String strUrl="http://maps.googleapis.com/maps/api/distancematrix/json?origins="; //trajet strUrl +=scr+"&destinations="+dest+"&mode=driving&language=fr-FR&sensor=false"; StringBuilder response = new StringBuilder(); try { URL url=new URL(strUrl); try { HttpURLConnection httpconn = (HttpURLConnection)url.openConnection(); if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192); String strLine = null; while ((strLine = input.readLine()) != null) { response.append(strLine + "\n"); } input.close(); } String jsonOutput = response.toString(); Add=jsonOutput; try { JSONObject jsonObject = new JSONObject(jsonOutput); JSONObject rowsObject = jsonObject.getJSONObject("rows"); //JSONArray elementsObject = rowsObject.getJSONArray(2); //***error*** // JSONObject durationObject = elementsObject.getJSONObject(1); // String duration = durationObject.getString("text"); //duree=duration; //dureeTrajet=Integer.parseInt(duration); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); info.setText("erreur 1"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); info.setText("erreur 2"); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Partager