简体   繁体   中英

bufferedReader reader.readline directly goes back to null

I'm using a bufferedReader and strangely, after i do this

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

Could anyone explain me what i do wrong or why this happens?

EDIT : i get json from the url which is this

{"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,

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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