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
| private void connexion_BDD_online() {
InputStream is = null;
StringBuilder sb=null;
String result=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://warcraft3campagne.free.fr/connexion.php?p=" + case_couleur() + "&s=" + case_essai());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.i("log_tag","a");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
Log.i("log_tag","b");
ListView lv= (ListView)findViewById(R.id.listview);
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"rowid", "Pseudo", "Score", "Date"};
int[] to = new int[] {R.id.item1, R.id.item2, R.id.item3, R.id.item4};
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", Integer.toString(i+1) + ".");
map.put("Pseudo", json_data.getString("name"));
map.put("Score", Integer.toString(json_data.getInt("score")));
map.put("Date", json_data.getString("date"));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
Log.i("log_tag","c");
}
catch(JSONException e1){
Log.e("log_tag", "Error1 "+e1.toString());
} catch (ParseException e1) {
Log.e("log_tag", "Error2 "+e1.toString());
}
} |
Partager