giovedì 28 giugno 2012

Caricamento dati in un BufferedReader da un ip locale/remoto con dialog "Loading..."

All'evento onCreate di una activity possiamo fare la richiesta ad un ip (ad esempio con asp, jsp, etc.) e alimentare una ArrayList di "Book", utile per un eventuale popolamento di un "SimpleAdapter".
private ArrayList<Map<String,String>> lista;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    loadData();
}
    
public void loadData(){
    try{
        new LoadingData().execute("");
    }catch(Exception e){
        //...
    }
}
    
private class LoadingData extends AsyncTask<String, Void, Void>{
    private ProgressDialog Dialog = new ProgressDialog(Main.this);  
    
    protected void onPreExecute() {   
        Dialog.setMessage("Loading...");   
        Dialog.show();   
    }  
    
    protected Void doInBackground(String... urls) {   
        lista = new ArrayList<Map<String,String>>();
        List<Book> listBook = new ArrayList<Book>();
        
        ArrayList<NameValuePair> nameValuePairs = 
new ArrayList<NameValuePair>();
        try {
            //esempio di parametro che si aspetta un file jsp o asp
            nameValuePairs.add(new BasicNameValuePair("action", "select"));
            
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams params = httpclient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 5);
            HttpConnectionParams.setSoTimeout(params, 5);

            HttpPost httppost = new HttpPost("http://" + "192.168.xxx.xxx");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //risposta della jsp o asp
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            
            BufferedReader reader = new BufferedReader(new 
InputStreamReader(is, "iso-8859-1"), 8);
            String line = null;
            Book Book;
            /*
            immaginando delle righe tipo: 
                I promessi sposi|9
                La divina commedia|10
                ...
            scorro i record e carico la lista
            */
            while ((line = reader.readLine()) != null) {
                 String[] result=line.split("\\|");
                 String desc =result[1];
                 String amount = result[2];
                 Book = new Book();
                 Book.setDescription(desc);
                 Book.setAmount(amount);
                 listBook.add(Book);
            }
            is.close();
        }
        catch (ClientProtocolException e)
        {    
        return null;
        }
        catch (IOException e)
        {  
        return null;
        }
        catch (Exception e)
        {  
        return null;
        }

        for (Book Book : listBook) {
            Map<String,String> data = new HashMap<String,String>();
            data.put(DESC,Book.getDescription());
            data.put(AMOUNT,Book.getAmount());
            lista.add(data);
        }
        
        return null;
    }
    
    protected void onPostExecute(Void unused) {   
        Dialog.dismiss();  
        //... eventuale popolamento di un SimpleAdapter
    }
}