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
| import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
public class MainActivity extends Activity {
Actualité oActualite;
ArrayList<Actualité> listeActualites;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
StringBuffer stringBuffer = new StringBuffer("");
BufferedReader bufR = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet requete = new HttpGet();
URI uri = new URI("http://192.168.56.1/dev_simag/envoi.php");
requete.setURI(uri);
HttpResponse reponse = client.execute(requete);
InputStream is = reponse.getEntity().getContent();
bufR = new BufferedReader(new InputStreamReader(is));
String ligneLue = bufR.readLine();
while (ligneLue != null) {
stringBuffer.append(ligneLue);
stringBuffer.append("\n");
ligneLue = bufR.readLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufR != null) {
try {
bufR.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
try {
JSONArray jArray = new JSONArray(stringBuffer.toString());
for (int i = 0; i < jArray.length(); i++) {
oActualite = new Actualité();
oActualite.setOcontenu(jArray.getJSONObject(i).getString("contenu").toString());
oActualite.setoChemin(jArray.getJSONObject(i).getString("chemin").toString());
}
} catch (JSONException jex) {
jex.printStackTrace();
}
if (listeActualites != null) {
ActualiteAdapter adapter = new ActualiteAdapter(this, listeActualites);
ListView listeVueActualites = findViewById(R.id.listeActualite);
listeVueActualites.setAdapter(adapter);
}
}
} |