Android 2.3.3 中的 Http 获取请求 [英] Http get request in Android 2.3.3

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

问题描述

我需要关于发送 HTTP GET 请求的帮助.我的代码如下:

I need help with sending HTTP GET request. My code is as follows:

    URL connectURL = new URL("url");
    HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

    conn.setDoInput(true); 
    conn.setDoOutput(true); 
    conn.setUseCaches(false); 
    conn.setRequestMethod("GET"); 

    conn.connect();
    conn.getOutputStream().flush();      
    String response = getResponse(conn);

但是它在 getResponse(conn); 为什么失败了?

But it fails at getResponse(conn); Why?

推荐答案

GET 请求可以这样使用:

GET request could be used like this:

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://www.google.com";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
        // do something with the response
        String response = EntityUtils.toString(resEntityGet);
        Log.i("GET RESPONSE", response);
    }
} catch (Exception e) {
    e.printStackTrace();
}

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

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