发送HTTP GET请求我需要帮助.我的代码如下:
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);
Run Code Online (Sandbox Code Playgroud)
但它失败了getResponse(conn);为什么?
Can*_*ner 15
GET请求可以像这样使用:
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();
}
Run Code Online (Sandbox Code Playgroud)