2013-03-25 78 views
-1

我在找工作HtttpRequest類,這樣我可以做這樣的:Android平臺HttpRequest類

String response = Request.get("http://google.com"); 

我已經寫了一個類,但它不Android 3+工作,但在2.3確實。

public class WebRequest { 
public String get(String url){ 
    HttpClient httpclient = new DefaultHttpClient(); 
    try { 
     HttpGet httpget = new HttpGet(url); 

     // Create a response handler 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     String responseBody = httpclient.execute(httpget, responseHandler); 


     return responseBody; 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     return null; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 

}

請幫助!

+0

是在2.3的工作?如果是這樣檢查錯誤是嚴格模式策略,如果這樣將其轉換爲異步任務 – 2013-03-25 12:23:07

+0

它可能與編碼有關,嘗試將它傳遞給UTF-8編碼的URL – meh 2013-03-25 12:24:08

+0

請顯示代碼,這是行不通的。 – 2013-03-25 12:27:47

回答

1

你可以看看這個答案:How do I use the Simple HTTP client in Android?

在那裏,他們用這兩種方法:

public static void 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 the response does not enclose an entity, there is no need 
     // to worry about connection release 

     if (entity != null) { 

      // A Simple JSON Response Read 
      InputStream instream = entity.getContent(); 
      String result= convertStreamToString(instream); 
      // now you have the string representation of the HTML request 
      instream.close(); 
     } 


    } catch (Exception e) {} 
} 

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(); 
} 

UPDATE

使用的AsyncTask:Android HTTP Request AsyncTask

更新2 - 簡版

public class XmlTask extends AsyncTask<String, Void, String>{ 

    public String doInBackground(String... urls){ 
     String url = urls[0]; 

     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 the response does not enclose an entity, there is no need 
      // to worry about connection release 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       String result= convertStreamToString(instream); 
       // now you have the string representation of the HTML request 
       instream.close(); 
      } 

       return xml; 
      } 
    } 

    public void onPostExecute(String xml){ 
     // Your XML parsing statement here 
    } 
} 

創建這個類(和創建自己的XML解析器?!)使用後如下:

String result = new XmlTask().execute("http://google.com"); 
+0

我想你必須在Android 3+上使用AsyncTask。 – Oskar 2013-03-25 13:07:41

+0

也許這就是你要找的@ user1849921? [Android HTTP Request AsyncTask](http://stackoverflow.com/a/8829321/2140191) – 2013-03-25 13:20:45

+0

是的,但我如何直接返回響應,我可以繼續我開始執行的地方? – Oskar 2013-03-25 13:25:17