在Android中下载网址

时间:2016-02-03 14:30:41

标签: java android download

我尝试从网址http://google.com和其他网址下载HTML内容。

我试过了:

URL url = new URL("http://google.com/");
urlConnection = (HttpURLConnection) url.openConnection();

虽然我已经尝试InputStream,但它的效果并不相同。

try {

    URL url = new URL("http://google.com/");

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        System.out.print(urlConnection.getResponseCode());


    }
    catch (Exception e)
    {
        System.out.print("Error: "+e.getMessage());
    }
    finally {
        urlConnection.disconnect();
    }

最后,我看到catche.getMessagenull。如果我进行调试,urlConnection.getResponseCode()将返回-1

1 个答案:

答案 0 :(得分:0)

您将DoOutput设置为不进行输出的位置。您没有在输入的位置设置DoInput。删除分块流模式。