1
 public class PerformNetworkTasks extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            BufferedReader reader = null;

            try {
                URL url = new URL(params[0]); 
                connection = (HttpURLConnection) url.openConnection();
                connection.connect(); //getting the connection to the URL to read JSON data
                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }

                String jsonText = buffer.toString(); // gets what the URL returns as JSON



                JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON

                List<String> allInfo = new ArrayList<String>(); // list to put all the returned information

                JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from
                for (int i = 0; i < linemanques.length(); i++) {

                        JSONObject questionParts = linemanques.getJSONObject(i);
                        quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
                        questype = questionParts.getString("questype");
                        question = questionParts.getString("question");
                        ans1 = questionParts.getString("ans1");
                        ans2 = questionParts.getString("ans2");
                        ans3 = questionParts.getString("ans3");
                        ans4 = questionParts.getString("ans4");
                        correctans = questionParts.getString("correctans");
                        category = questionParts.getString("category");
                        notes = questionParts.getString("notes");
                        flag = questionParts.getString("flag");


                    allInfo.add(quesnum);    
                    allInfo.add(questype);
                    allInfo.add(question);
                    allInfo.add(ans1);
                    allInfo.add(ans2);
                    allInfo.add(ans3);
                    allInfo.add(ans4);
                    allInfo.add(correctans);
                    allInfo.add(category);
                    allInfo.add(notes);
                    allInfo.add(flag);
                    allInfo.add("\n");
                }

                 return allInfo.toString(); 
                 /* 
                  right now I am returning the list as a String, 
                  so that I can actually view the data. 
                  I need to put this data into their own TextViews. 
                  So how can I return the list I have so that I can set
                  the individual TextViews as one section from the list?  
                  */     

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
            }
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;

        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            inputDataTV.setText(result);
        }

I need to return some data individually. So I need to return an array (i think) so that I can set the TextView as e.g. arrays.get(number).

Is there some other way that I am not realizing here, or should I continue with what I am doing to get the data individually?

Just to add, I am getting the info from a website.

4
  • Can you change the code? You could just change the method signature to return List<String> and then return allInfo instead of return allInfo.toString() - is that what you are after? Commented Jan 9, 2020 at 2:05
  • I will try this, thanks for the suggestion. Commented Jan 9, 2020 at 2:07
  • Btw, the incoming String... params only uses the first one (in position 0 of the array) - you could change this to simply String urlString and then craft your URL like URL url = new URL(urlString); Commented Jan 9, 2020 at 2:08
  • You're right, I don't know why I did it like that. Commented Jan 9, 2020 at 2:09

1 Answer 1

1

You can return any data type you want but your AsyncTask structure should be based on result data type

  public class PerformNetworkTasks extends AsyncTask<String, String, List<String>/*resultParam*/> {

    @Override
    protected List<String>/*will same as result parma*/ doInBackground(String... params) {
        return null;/*now you can return list of string*/
    }

    @Override
    protected void onPostExecute(List<String>/*finally receive result*/ result) {
        super.onPostExecute(result);
    }
}

so your code will be

 public class PerformNetworkTasks extends AsyncTask<String, String, List<String>> {

    @Override
    protected List<String> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect(); //getting the connection to the URL to read JSON data
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();

            String line = "";
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            String jsonText = buffer.toString(); // gets what the URL returns as JSON


            JSONObject obj = new JSONObject(jsonText); // using JSONObject to pass to a JSONArray to search for the JSON

            List<String> allInfo = new ArrayList<>(); // list to put all the returned information

            JSONArray linemanques = obj.getJSONArray("linemanques"); //selects the array to read from
            for (int i = 0; i < linemanques.length(); i++) {

                JSONObject questionParts = linemanques.getJSONObject(i);
                quesnum = questionParts.getString("quesnum"); // all of questionParts.getString() are for getting the data in the JSONArray
                questype = questionParts.getString("questype");
                question = questionParts.getString("question");
                ans1 = questionParts.getString("ans1");
                ans2 = questionParts.getString("ans2");
                ans3 = questionParts.getString("ans3");
                ans4 = questionParts.getString("ans4");
                correctans = questionParts.getString("correctans");
                category = questionParts.getString("category");
                notes = questionParts.getString("notes");
                flag = questionParts.getString("flag");


                allInfo.add(quesnum);
                allInfo.add(questype);
                allInfo.add(question);
                allInfo.add(ans1);
                allInfo.add(ans2);
                allInfo.add(ans3);
                allInfo.add(ans4);
                allInfo.add(correctans);
                allInfo.add(category);
                allInfo.add(notes);
                allInfo.add(flag);
                allInfo.add("\n");
            }

            return allInfo;
             /*
              right now
              I am returning the list as a String,
              so that I can actually view the data.
              I need to put this data into their own TextViews.
              So how can I return the list I have so that I can set
              the individual TextViews as one section from the list?
              */

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
        }
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }


    @Override
    protected void onPostExecute(List<String> result) {
        super.onPostExecute(result);
        inputDataTV.setText(result.get(0));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I had tried this but had edited the wrong spot in the PerformNetworkTasks method signature. I went to watch a youtube video and figured out that it is formed <params, progress, result>. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.