2010-09-09 7 views
5

Sto usando la seguente classe per connettermi al mio servizio web. Mi piacerebbe rendere questo asincrono. Come posso fare questo?Come posso effettuare connessioni URL asincrone su Android?

package org.stocktwits.helper; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class RestClient{ 

    private static String convertStreamToString(InputStream is) { 
     /* 
     * To convert the InputStream to String we use the BufferedReader.readLine() 
     * method. We iterate until the BufferedReader return null which means 
     * there's no more data to read. Each line will appended to a StringBuilder 
     * and returned as String. 
     */ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return sb.toString(); 
    } 


    /* This is a test function which will connects to a given 
    * rest service and prints it's response to Android Log with 
    * labels "Praeda". 
    */ 
    public static JSONObject connect(String url) 
    { 

     HttpClient httpclient = new DefaultHttpClient(); 


     // Prepare a request object 
     HttpGet httpget = new HttpGet(url); 

     // Execute the request 
     HttpResponse response; 
     try { 
      response = httpclient.execute(httpget); 
      // Examine the response status 
      Log.i("Praeda",response.getStatusLine().toString()); 

      // Get hold of the response entity 
      HttpEntity entity = response.getEntity(); 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       String result= convertStreamToString(instream); 

       // A Simple JSONObject Creation 
       JSONObject json=new JSONObject(result); 

       // Closing the input stream will trigger connection release 
       instream.close(); 

       return json; 
      } 


     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return null; 
    } 

} 
+0

Vedi http://stackoverflow.com/questions/1014528/asynchronous-http-client-for-java – Ladlestein

risposta

4

In aggiunta a tutte le possibili soluzioni in commento di Ladlestein, c'è la risposta più semplice di avvolgere tutto ciò che in un AsyncTask.

+0

Hi. So che questa è una domanda molto vecchia, e successivamente una vecchia risposta, ma ti dispiacerebbe aggiornare il progetto di esempio per fare richieste HTTP asincrone nel framework Android? Grazie! –

Problemi correlati