java.net.SocketException:recvfrom失败:EBADF(错误文件描述符)Android

Igo*_*aev 2 java url android json socketexception

我尝试从 Android 项目中的 URL 读取 json 文件。但奇怪的情况发生了。StringBuilder 成功获取前几个字符,但随后我得到 java.net.SocketException: recvfrom failed: EBADF (Bad file detector)

为什么我会得到这个异常?

我的代码有什么问题吗?)

JSON: http: //inlyfortesting.ucoz.net/artists.json

我的 JSON 解析器:

    public JSONArray getJSONFromUrl(String urlAsString) {
        // try to create JSONObject from string
        try {
            URL url = new URL(urlAsString);
            new DownloadFileTask().execute(url).get();
            jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return jObj;
    }

    private class DownloadFileTask extends AsyncTask<URL, Integer, Long> {
        StringBuilder sb;
        protected Long doInBackground(URL... urls) {
            HttpURLConnection connection = null;
            try {
                connection = (HttpURLConnection) urls[0].openConnection();
                is = new BufferedInputStream(connection.getInputStream());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                connection.disconnect();
            }

            //file reading
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
                sb = new StringBuilder();
                int symbol;
                int counter = 0;
                while ((symbol = reader.read()) != -1) { //PROBLEM HERE
                    sb.append((char)symbol); //OR HERE
                }
                json = sb.toString();
                is.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace(); //java.net.SocketException: recvfrom failed: EBADF (Bad file descriptor)
            }
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

use*_*421 5

在读取数据之前,您将断开连接。你需要事后这样做。

这当然是显而易见的吗?