Android_http_get

         在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost。这一篇一个实例给出httpGet的使用方法:

public class HttpGetDemo extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
  
        BufferedReader in = null;  
        try {  
            HttpClient client = new DefaultHttpClient();  
            HttpGet request = new HttpGet("http://www.baidu.com");  
            HttpResponse response = client.execute(request);  
            in = new BufferedReader(  
                    new InputStreamReader(  
                        response.getEntity().getContent()));  
  
            StringBuffer sb = new StringBuffer("");  
            String line = "";  
            String NL = System.getProperty("line.separator");  
            while ((line = in.readLine()) != null) {  
                sb.append(line + NL);  
            }  
            in.close();  
  
            String page = sb.toString();  
            System.out.println(page);  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } finally {  
            if (in != null) {  
                try {  
                    in.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
}  


你可能感兴趣的:(Android_http_get)