Android 6.0 HtppURLConnection崩溃错误

时间:2016-06-29 17:48:29

标签: android httpurlconnection

我在asynctask中使用此代码来加载来自URL的数据。我在manifest.xml中添加了internet权限,我在Android Studio上没有任何错误,但是当我调试应用程序时,崩溃了。我的错在哪里?

protected Void doInBackground(Void... params) {
    try {
        URL url = new URL("my_url");
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        buffer = new StringBuffer();
        String line = "";
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        jsonString = buffer.toString();
        if(connection != null) { connection.disconnect(); }
        try {
            if(reader != null) { reader.close();  }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

谢谢..

0 个答案:

没有答案