【发布时间】:2016-08-20 12:55:12
【问题描述】:
我有一个使用 DefaultHttpClient 执行 http 请求的旧代码,我正在尝试将其转换为 HttpURLConnection,但我遇到了响应问题。
这是原始代码:
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
这就是我想要做的:
HttpURLConnection conn = null;
URL url;
try
{
url = new URL(serviceUrl);
}
catch (MalformedURLException e)
{
throw new IllegalArgumentException("invalid url: " + serviceUrl);
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
//Don't know what to do now to return the response(?)
}
但我不知道如何做才能获得与响应相同的结果。
【问题讨论】:
-
使用
conn.getInputStream() -
您想要一种方法来获取响应吗?
-
@Enzokie 好的,谢谢
标签: java android http response httpurlconnection