Running multiple AsyncTasks at the same time — not possible?

前端 未结 7 819
别那么骄傲
别那么骄傲 2020-11-21 13:33

I\'m trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here\'s a simple snippet to describe my probl

7条回答
  •  [愿得一人]
    2020-11-21 13:34

    It is posible. My android device version is 4.0.4 and android.os.Build.VERSION.SDK_INT is 15

    I have 3 spinners

    Spinner c_fruit=(Spinner) findViewById(R.id.fruits);
    Spinner c_vegetable=(Spinner) findViewById(R.id.vegetables);
    Spinner c_beverage=(Spinner) findViewById(R.id.beverages);
    

    And also I have a Async-Tack class.

    Here is my spinner loading code

    RequestSend reqs_fruit = new RequestSend(this);
    reqs_fruit.where="Get_fruit_List";
    reqs_fruit.title="Loading fruit";
    reqs_fruit.execute();
    
    RequestSend reqs_vegetable = new RequestSend(this);
    reqs_vegetable.where="Get_vegetable_List";
    reqs_vegetable.title="Loading vegetable";
    reqs_vegetable.execute();
    
    RequestSend reqs_beverage = new RequestSend(this);
    reqs_beverage.where="Get_beverage_List";
    reqs_beverage.title="Loading beverage";
    reqs_beverage.execute();
    

    This is working perfectly. One by one my spinners loaded. I didn't user executeOnExecutor.

    Here is my Async-task class

    public class RequestSend  extends AsyncTask {
    
        private ProgressDialog dialog = null;
        public Spinner spin;
        public String where;
        public String title;
        Context con;
        Activity activity;      
        String[] items;
    
        public RequestSend(Context activityContext) {
            con = activityContext;
            dialog = new ProgressDialog(activityContext);
            this.activity = activityContext;
        }
    
        @Override
        protected void onPostExecute(String result) {
            try {
                ArrayAdapter adapter = new ArrayAdapter (activity, android.R.layout.simple_spinner_item, items);       
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spin.setAdapter(adapter);
            } catch (NullPointerException e) {
                Toast.makeText(activity, "Can not load list. Check your connection", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (Exception e)  {
                Toast.makeText(activity, "Can not load list. Check your connection", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
            super.onPostExecute(result);
    
            if (dialog != null)
                dialog.dismiss();   
        }
    
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.setTitle(title);
            dialog.setMessage("Wait...");
            dialog.setCancelable(false); 
            dialog.show();
        }
    
        @Override
        protected String doInBackground(String... Strings) {
            try {
                Send_Request();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            return null;
        }
    
        public void Send_Request() throws JSONException {
    
            try {
                String DataSendingTo = "http://www.example.com/AppRequest/" + where;
                //HttpClient
                HttpClient httpClient = new DefaultHttpClient();
                //Post header
                HttpPost httpPost = new HttpPost(DataSendingTo);
                //Adding data
                List nameValuePairs = new ArrayList(2);
    
                nameValuePairs.add(new BasicNameValuePair("authorized","001"));
    
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);
    
                BufferedReader reader;
                try {
                    reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    StringBuilder builder = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line) ;
                    }
    
                    JSONTokener tokener = new JSONTokener(builder.toString());
                    JSONArray finalResult = new JSONArray(tokener);
                    items = new String[finalResult.length()]; 
                    // looping through All details and store in public String array
                    for(int i = 0; i < finalResult.length(); i++) {
                        JSONObject c = finalResult.getJSONObject(i);
                        items[i]=c.getString("data_name");
                    }
    
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题