简体   繁体   中英

Error while parsing JsonObject

I am trying to get JsonObject from DownloadTask. But I am getting array values that are stored in downloadData along with JsonObjects stored in jsonObjectText. Look at below in logs. Why my code return array with JsonObject , But I only log the content of the jsonObject variable.

Here picture of Logs:

日志

Here is my Code :

package com.example.smarpitsingh.webview;  

public class MainActivity extends AppCompatActivity {  

    String name = "Result";
    String result = "",downloadedData,jsonObjectText;
    URL url;
    HttpURLConnection httpURLConnection;
    InputStream inputStream;
    InputStreamReader streamReader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask downloadTask = new DownloadTask();
        try {
            downloadedData = downloadTask.execute("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty").get();

           JSONArray jsonArray = new JSONArray(downloadedData);

           for (int i=1; i<20; i++){

               DownloadTask jasonTask = new DownloadTask();
                jsonObjectText = jasonTask.execute("https://hacker-news.firebaseio.com/v0/item/"+jsonArray.getString(i)+".json?print=pretty").get();

               Log.i(name,jsonObjectText);
            }


        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public class DownloadTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            try {
                url = new URL(urls[0]);
                httpURLConnection = (HttpURLConnection)url.openConnection();
                inputStream = httpURLConnection.getInputStream();
                streamReader = new InputStreamReader(inputStream);

                int data = streamReader.read();
                while (data != -1){
                    char count = (char)data;
                    result += count;
                    data = streamReader.read();
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return result;
        }
    }
`}`

Because you have got an array in your json: 在此处输入图片说明

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