2013-03-25 80 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