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
| package com.tt;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class AhActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
}
//findViewById(R.id.button1).setOnClickListener( new OnClickListener(){
private String result;
//JSONArray json_data;
//@Override
//public void onClick(View arg0) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();{
List<Map<String,Object>> res = new ArrayList<Map<String,Object>>();
// Envoie de la commande http
try{
HttpClient httpclient = new DefaultHttpClient();
String strURL="http://testandroid.hebergratuit.com/phpandroid.php";
HttpPost httppost = new HttpPost(strURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
this.result=sb.toString();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Parse les données JSON
String returnString = null;
try{
// JSONArray J = new JSONArray(result);
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Map<String,Object> map = new HashMap<String,Object>();
//JSONObject json_data = jArray.getJSONObject(i);
// Résultats de la requête
Log.i("log_tag",
returnString=json_data.getString("adresse")
);
map.put("ville",json_data.getString("adresse"));
//returnString += "\n\t" + jArray.getJSONObject(i);
res.add(map);
}
// ListView lv = (ListView)findViewById(R.id.list);
//TextView tv = (TextView) findViewById(R.id.item_title);
//tv.setText(returnString);
//System.out.println("resultat: "+returnString);
} catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
} //return returnString;
ListView lv = (ListView)findViewById(R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, res , R.layout.main,
new String[] { "ville"},
new int[] { R.id.item_title});
lv.setAdapter(adapter);
}
} |
Partager