Android中的HTTP连接 [英] HTTP connection in Android

查看:116
本文介绍了Android中的HTTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建Http连接以将网页内容检索到我的android?请为此发布示例代码.

How to create an Http Connection to retrive a web page content to my android? Please post example code for this.

推荐答案

它的http客户端,获取示例可能会有所帮助

Its http client,get example it might help

    HttpClient client;
    HttpGet method;
    String url;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    url="enter your url here";
    method = new HttpGet(url);
    try {
            client.execute(method);


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           setContentView(R.layout.main);
}
public void executeHttpGet() throws Exception {
    BufferedReader in = null;
    try {
        client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        HttpResponse response = client.execute(method);
        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);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

我可以在此示例中尝试访问预览中的XML数据.

it access XML data in preview i have this example try.

这篇关于Android中的HTTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆