简体   繁体   English

bufferedReader reader.readline 直接回null

[英]bufferedReader reader.readline directly goes back to null

I'm using a bufferedReader and strangely, after i do this我正在使用 bufferedReader 并且奇怪的是,在我这样做之后

reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

EDIT3: the BufferedReader reader itself is know fully loaded and correct, but the reader.readline, which is needed, goes directly back to null EDIT3: BufferedReader 阅读器本身知道完全加载且正确,但需要的 reader.readline 直接返回 null

Could anyone explain me what i do wrong or why this happens?谁能解释一下我做错了什么或为什么会发生这种情况?

EDIT : i get json from the url which is this编辑:我从 url 得到 json,这是这个

{"sports":[{"sport":{"id":"1","name":"yoga","rating":"1.6"}},{"sport":{"id":"2","name":"tennis","rating":"3.6"}},{"sport":{"id":"3","name":"zwemmen","rating":"4.7"}}],"view":{"name":"SportData","display":"page_1","path":"admin/content/data/view/SportData","root":"sports","child":"sport","pages":null,"page":null,"count":3,"limit":null}} {"sports":[{"sport":{"id":"1","name":"yoga","rating":"1.6"}},{"sport":{"id":"2 ","name":"tennis","rating":"3.6"}},{"sport":{"id":"3","name":"zwemmen","rating":"4.7"} }],"view":{"name":"SportData","display":"page_1","path":"admin/content/data/view/SportData","root":"sports","child ":"sport","pages":null,"page":null,"count":3,"limit":null}}

So when i debug this, i get following....所以当我调试这个时,我得到以下......

运行缓冲阅读器 运行 reader.readline

EDIT2: this is the full code for this, EDIT2:这是完整的代码,

@Override
    protected Void doInBackground(String... type) {
        URL_STRING = "my json_url";
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            try
            {
                URL url;
                StringBuilder stringBuilder;
                    url = new URL(URL_STRING);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.connect();
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    stringBuilder = new StringBuilder();
                    while ((line =reader.readLine()) != null)
                    {
                        stringBuilder.append(line+"\n");
                    }
                    result =  stringBuilder.toString();
                }
                catch (Exception e)
                {
                }
        }
        return null;
    }

Your code doesn't make sense.你的代码没有意义。 You're appending the same line forever.你永远追加同一行。 Your loop should be:你的循环应该是:

while ((line = reader.readLine()) != null)
{
    stringBuilder.append(line+"\n");
}

I'm also wondering whether you're creating two BufferedReaders on the same socket, which doesn't make sense either.我还想知道您是否在同一个套接字上创建两个BufferedReaders ,这也没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM