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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package com.example.myapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.*;
import com.example.myapp.MyActivity;
import com.example.myapp.heure;
import com.example.myapp.Voiture;
import com.example.myapp.Moniteur;
import com.example.myapp.JSONAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Etudiant on 15/12/2015.
*/
public class MyActivity extends Activity {
heure p = null;
ListView mListView;
List<Voiture> tweets = new ArrayList<Voiture>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (reseauOK()) {
new JSONParse().execute();
mListView = (ListView) findViewById(R.id.listrdv);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
/* changer de VIEW AU CLICK*/
@Override
public void onItemClick(AdapterView<?> heure, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
Voiture itemTweet = (Voiture) mListView.getItemAtPosition(position);
String marqueV = itemTweet.getMarqueV();
String PlaqueImmatriculationV = itemTweet.getPlaqueImmatriculationV();
Intent intent = new Intent(MyActivity.this, heure.class);
intent.putExtra("idR", p.getId());
intent.putExtra("idV", p.getVoiture().get(position).getIdV());
intent.putExtra("marqueV", p.getVoiture().get(position).getMarqueV());
intent.putExtra("PlaqueImmatriculationV", p.getVoiture().get(position).getPlaqueImmatriculationV());
startActivity(intent);
finish();
}
});
} else {
Context context = getApplicationContext();
CharSequence text = "Reseau mobile non disponible ";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
private List<Voiture> genererTweets() {
/* tweets.add(new Tweet(Color.BLUE, "Kevin", "C'est ici que �a se passe !"));
tweets.add(new Tweet(Color.GREEN, "Logan", "Que c'est beau..."));
tweets.add(new Tweet(Color.RED, "Mathieu", "Il est quelle heure ??"));
tweets.add(new Tweet(Color.GRAY, "Willy", "On y est presque"));*/
return tweets;
}
private boolean reseauOK() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
return true;
else
return false;
}
private class JSONParse extends AsyncTask<Void, Void, Void> {
private ProgressDialog pDialog;
private boolean erreurWS = false;
// Bundle bundle = getIntent().getExtras();
//int idR = bundle.getInt("idR");
@Override
protected void onPreExecute() {
super.onPreExecute();
//Gestion de la progressBar
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Veuillez patienter");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... args) {
JSONObject jsonProduits = lireJson(); //r�cup�ration du Json
if (!erreurWS)
jsonToTextView(jsonProduits); //mise � jour de la TextView
return null;
}
@Override
protected void onPostExecute(Void result) {
pDialog.dismiss();
if (erreurWS) {
Context context = getApplicationContext();
CharSequence text = "Erreur lors de l'accés au service Web";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
//mListView = (ListView) findViewById(R.id.listView);
List<Voiture> tweets = genererTweets();
JSONAdapter adapter = new JSONAdapter(MyActivity.this, tweets);
mListView.setAdapter(adapter);
}
}
private JSONObject lireJson() {
String myurl = "http://www.siomende.fr/anoll/Vala/ws/ws.php?action=getRdv";
InputStream is = null;
String result = "";
JSONObject jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(myurl);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
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();
result = sb.toString();
jArray = new JSONObject(result);
} catch (Exception e) {
erreurWS = true;
e.printStackTrace();
}
return jArray;
}
int n = 0;
private void jsonToTextView(JSONObject json) {
try {
// Getting JSON Object
JSONObject c = json;
// Storing JSON item in a Variable
int idR = c.getInt("idR");
String lieuR = c.getString("lieuR");
String dateR = c.getString("dateR");
String dureeR = c.getString("dureeR");
p = new heure(lieuR, dateR, dureeR);
p.setId(idR);
genererTweets();
for (int j = 0; j < c.getJSONArray("voiture").length(); j++) {
JSONObject e = c.getJSONArray("voiture").getJSONObject(j);
int idV = e.getInt("idV");
String plaque = e.getString("plaque");
String marque = e.getString("marque");
Voiture voiture = new Voiture(plaque, marque);
voiture.setIdV(idV);
genererTweets();
tweets.add(new Voiture(voiture.getMarqueV(), voiture.getPlaqueImmatriculationV()));
p.ajoutVoiture(voiture);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} |
Partager