Bonsoir,
j'ai integré la librairie Gson et pourtant la classe com.google.gson.Gson n'a pas été retrouvé lors de l'exécution de mon programme.
J'essaye de calculer les distances entre les differents points de la Map Google.
Ou peut être le problème?
merci
voiçi le code:
Main
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67 import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GoogleDistance { private final String URL = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=Bouaye,%20Loire-Atlantique&destinations=C.H.U.%20Saint-Jacques,%2044200%20Quartiers%20Sud,%20Nantes|Saint-Mars-de-Coutais|47.123819,-1.66783|Saint-Herblain&sensor=false"; Gson gson; public GoogleDistance() { gson = new Gson(); } private InputStream sendRequest(URL url) throws Exception { try { // Ouverture de la connexion HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // Connexion � l'url urlConnection.connect(); // Si le serveur nous r�pond avec un code OK if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { Log.i("connexion", "succès"); return urlConnection.getInputStream(); } } catch (Exception e) { throw new Exception(""); } return null; } public List<GoogleDistanceMatrix> getDistanceMatrix() { try { // Envoie de la requ�te InputStream inputStream = sendRequest(new URL(URL)); // V�rification de l'inputStream if(inputStream != null) { // Lecture de l'inputStream dans un reader InputStreamReader reader = new InputStreamReader(inputStream); // Return la liste d�s�rialis� par le moteur gson return gson.fromJson(reader, new TypeToken<List<GoogleDistanceMatrix>>(){}.getType()); } } catch (Exception e) { Log.e("WebService", "Impossible de rapatrier les donn�es :("); } return null; } }
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 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.localisation); listViewData = (ListView) findViewById(R.id.listViewData); getData(); listViewData.setOnItemClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void getData() { new AsyncTask<Void, Void, List<GoogleDistanceMatrix>>() { @Override protected List<GoogleDistanceMatrix> doInBackground(Void... params) { GoogleDistance webService = new GoogleDistance(); List<GoogleDistanceMatrix> liste = webService.getDistanceMatrix(); if (liste != null) { return liste; } return new ArrayList<GoogleDistanceMatrix>(); }; protected void onPostExecute(java.util.List<GoogleDistanceMatrix> result) { pointAdapter = new DistanceAdapter(getBaseContext(), android.R.layout.simple_list_item_2, result); listViewData.setAdapter(pointAdapter); }; }.execute(); }
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 public class GoogleDistanceMatrix { String destination_address; String origin_address; String status; Row[] row; public String getDestination_address() { return destination_address; } public void setDestination_address(String destination) { this.destination_address = destination; } public String getOrigin_address() { return destination_address; } public void setOrigin_address(String destination) { this.origin_address = destination; } public Row[] getRow() { return row; } public void setRow(Row[] row) { this.row = row; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 12-23 18:31:19.035: E/dalvikvm(16985): Could not find class 'com.google.gson.Gson', referenced from method com.example.trainingcenter.GoogleDistance.<init> 12-23 18:31:19.035: W/dalvikvm(16985): VFY: unable to resolve new-instance 507 (Lcom/google/gson/Gson;) in Lcom/example/trainingcenter/GoogleDistance; 12-23 18:31:19.045: E/dalvikvm(16985): Could not find class 'com.example.trainingcenter.GoogleDistance$1', referenced from method com.example.trainingcenter.GoogleDistance.getDistanceMatrix
Partager