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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
execute_web_service();
//get_liste_gares();
}
public void execute_web_service() {
progressd = ProgressDialog.show(carte_activity.this, "", "Chargement...", true,
false);
progressd.setCancelable(true);
Thread thread = new Thread(carte_activity.this);
thread.start();
}
@SuppressLint("HandlerLeak")
public void run() {
try
{
get_liste_magasins();
handler.sendEmptyMessage(0);
}
catch (Exception e)
{
Intent i=new Intent(carte_activity.this,Main.class);
i.putExtra("erreur","PAS DE MAGASINS DISPONILBES");
startActivity(i);
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
progressd.dismiss();
afficher_liste_magasins();
}
};
private List<magasin> results;
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public void get_liste_magasins(){
// String url="http://10.0.2.2:8888/get_list_magasins.php";
String url="http://mercure.comze.com/mercure/get_list_magasin.php";
Log.i("results magasins", "01");
InputStream source = retrieveStream(url);
String src = convertStreamToString(source);
Log.i("results magasins", "02");
Gson gson = new Gson();
// Reader reader = new InputStreamReader(source);
List_magasins response = null;
try{
response= gson.fromJson(src,
List_magasins.class);
}
catch (Exception e) {
// TODO: handle exception
}
Log.i("results excption BOOOOOOOOOOOOOOF!!!!!!", "03");
if(response !=null)
{
Log.i("results magasins", "04");
results = response.magasins;
Log.i("results magasins", "05");
}
Log.i("results magasins", ""+results.size());
// afficher_liste_gares();
}
//
public void afficher_liste_magasins(){
if(results!=null && results.size() > 0)
{
for(int i=0; i<results.size(); i++)
{
magasin g=results.get(i);
double latg=Double.parseDouble(g.latitude);
double longg=Double.parseDouble(g.longitude);
final LatLng local = new LatLng(latg,longg);
Marker locations = googleMap.addMarker(new MarkerOptions()
.position(local)
.title(g.nom)
.snippet(g.ville)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps)));
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(5);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
}
} |
Partager