HTTP get request in Android 2.3.3

I need help sending HTTP get requests. 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);

But it failed in getresponse (conn); Why?

resolvent:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>