| 12
 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
 
 | public class BDDAlarm extends Activity{
    /** Called when the activity is first created. */
	 public static final String strURL = "http://10.0.2.2:8080/mesRequestes/Alarm.php";    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncTask<Void,Void,List<Map<String,Object>>>()
        {
            protected List<Map<String,Object>> doInBackgroung(Void...voids )
            {
                List<Map<String,Object>> getServerData(String strURL)
                { 
             	   List<Map<String, Object>>  res = new ArrayList<Map<String, Object>>();
                    InputStream is = null;  
                    String result = "";  
                    ArrayList<NameValuePair> listAlr = new ArrayList<NameValuePair>();  
                    listAlr.add(new BasicNameValuePair("alarm",""));  
                    try{  
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(strURL);  
                        httppost.setEntity(new UrlEncodedFormEntity(listAlr));  
                        HttpResponse response = httpclient.execute(httppost);    
                        is = response.getEntity().getContent(); 
 
                    }catch(Exception e)
                    {  
                        Log.e("log_tag", "Error in http connection " + e.toString());  
                    }  
 
                    try{  
                    	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();  
                        Log.i("result", result);
                    }catch(Exception e)
                    {  
                        Log.e("log_tag", "Error converting result " + e.toString());  
                    }   
                    try{  
                       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>();
                           		map.put("ID_ALARM", json_data.getInt("ID_ALARM"));
                           		map.put("ID_OBJECT", json_data.getInt("ID_OBJECT"));
                           		res.add(map); 
                            Log.i("log_tag","ID_ALARM: "+json_data.getInt("ID_ALARM")+  
                                    ", ID_OBJECT: "+json_data.getInt("ID_OBJECT"));  
                        }  
                    }catch(JSONException e)
                    {  
                        Log.e("log_tag", "Error parsing data " + e.toString());  
                    }  
                   return res;   
                }  
        protected void onPostExecute(Long result)
        {
        	ListView list = (ListView)findViewById(R.id.listalarm);
        	list.setAdapter(new SimpleAdapter(this, res, R.layout.listitem,new String[]{"ID_ALARM","ID_OBJECT" }, new int[] { R.id.idAlarmTextView,R.id.idObjectTextView }));
        }  
      }    
   }.execute();  
}
} | 
Partager