DefaultHttpClient로 http 요청을 실행하는 오래된 코드가 있는데이를 HttpURLConnection로 변환하려고하지만 응답에 문제가 있습니다. 여기 HttpURLConnection이 DefaultHttpClient와 같은 응답을 반환합니다.
원래 코드 :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(?)
}
를하지만 같은 결과를 얻기 위해 그것을하는 방법을 모른다 응답과 마찬가지로. 이 코드에서
InputStream inputStream = conn.getInputStream();
StringBuilder build = new StringBuilder();
if (inputStream != null) {
InputStreamReader ISreader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(ISreader);
String line = reader.readLine();
while (line != null) {
build.append(line);
line = reader.readLine();
}
}
return build.toString();
, 당신은 InputStreamReader
및 BufferedReader
를 사용하여 응답을 얻을하는 데 사용되는 HttpUrlConnection
,에서 InputStream
을 받고 :
'conn.getInputStream()' – Enzokie
을 사용하여 ** 응답 **을 얻으시겠습니까? –
@Enzokie ok, thnks – PaulD