サーバーからファイル (Json) をダウンロードしようとしていますが、何らかの理由でコードが機能しません。マニフェストでアプリケーションのインターネット許可を与えました。URLは正しいです(少なくとも、単にコピーしてブラウザに貼り付けるだけで機能します)...誰かがここで何が問題なのかを確認できますか? ダウンロードを try/catch で囲みましたが、常に例外がスローされます。
事前に助けてくれてありがとう!!
private String downloadFile() throws IOException {
String toReturn = "";
URL url = new URL(urlDefault + itemToSearch);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
System.out.println("Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedInputStream is = new BufferedInputStream(
conn.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
toReturn += line;
}
br.close();
is.close();
conn.disconnect();
} else {
throw new IllegalStateException("HTTP response: " + responseCode);
}
return toReturn;
}