How to parse data from 2 different URLs by asyncTask method

前端 未结 3 1973
北恋
北恋 2021-01-02 18:03

I have an app that get data from a JSON URL. It works perfectly with one URL, but I need to get data from two JSON URLs at the same time. Like some data from one URL and som

相关标签:
3条回答
  • 2021-01-02 18:14

    Variant A) You could execute your GetJSONTask like this:

    new GetJSONTask().execute(url1, url2);
    

    And access the URLs in your AsyncTask like this:

     class GetJSONTask extends AsyncTask<String, Void, JSONObject[]> {
         ...
         JSONObject[] doInBackground(String... urls) {
    
             // Getting JSON from URL
            JSONObject json1 = jParser.getJSONFromUrl(urls[1]);
            JSONObject json2 = jParser.getJSONFromUrl(url2[2]);
    
            return new JSONObject[] {json1, json2};
        }
    
        void onPostExecute(JSONObject[] jsons) { ... }
    
    }
    

    Variant B) If you want to make the process running truly parallel, you could add another AsyncTask which creates 2 child Tasks and waits for their result. On most devices, this will half the time required to perform that task.

    class GetTwoJsonsTask extends AsyncTask<String, Void, JSONObject[]> {
        JSONObject[] doInBackground(urls) {
    
            task1 = new GetJSONTask().execute(urls[1]);
            task2 = new GetJSONTask().execute(urls[2]);
    
            // block until sub-tasks are finished
            JSONObject json1 = task1.get();
            JSONObject json2 = task2.get();
    
            return new JSONObject[] {json1, json2};
        }
    
        void onPostExecute(JSONObject[] jsons) { ... }
    
    }
    
    0 讨论(0)
  • 2021-01-02 18:20

    that's how i did it, it's in easy way to do it

    public class GetSetting extends AsyncTask<List<String>,Void,List<String>> {
    
        String urls[] =  new String[]{"http:// first url",
                                  "http:// second url"};
    
    
        @Override
        protected ArrayList<String> doInBackground(List<String>... lists) {
    
            HttpURLConnection urlConnection;
            InputStream inputStream;
            BufferedReader bufferedReader;
            URL urlIfSup;
            ArrayList<String> result = new ArrayList<>();
            try {
    
                // here to connect and get a different type of data  
    
                for (int i = 0; i < 2; i++){ // this one for connect the url from url array in every loop 
    
                    urlIfSup = new URL(urls[i]);
                    urlConnection = (HttpURLConnection) urlIfSup.openConnection();
                    inputStream = urlConnection.getInputStream();
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
    
                    String data = bufferedReader.readLine();
    
                    while(bufferedReader.readLine() != null){
    
                        data += bufferedReader.readLine();
                    }
                    if (i==0) {
    
                        // do something for the first url
    
                    }else if (i==1){
    
                      // do something for the second url
                       // use jSON if you want 
    
    
                      // then do this at the last url       
                        urlConnection.disconnect();
                        inputStream.close();
                        bufferedReader.close();
                    }
                }
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
            return result;
        }
    }
    
    0 讨论(0)
  • 2021-01-02 18:26

    I suggest you to change your method to

    class GetJSONTask extends AsyncTask<String, Void, JSONObject[]> {
    
     ...
    
     protected JSONObject[] doInBackground(String... urls) {
        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();
    
        // Getting JSON from URL
        JSONObject[] jsons = new JSONObject[2];
        jsons[0] = jParser.getJSONFromUrl(url1);
        jsons[1] = jParser.getJSONFromUrl(url2);
    
        return jsons;
    }
    protected void onPostExecute(JSONObject[] jsons) {
        JSONObject json1 = jsons[0];
        JSONObject json2 = jsons[1];
        // do you work after this
    }
    }
    

    hope this help!

    0 讨论(0)
提交回复
热议问题