0

I have a list that contains elements and i want to send each element of my list seperatly to a local database server using RestTemplate in a AsynkTask.If i specify which element i want to send, i can send it easily and works with my AsyncTask but i want to loop all the list and send all elements one by one to my database..i tried to do a "for loop" inside my AsyncTask but that won't work, i also tried to call the AsyncTask in "for loop" and again nothing happens.. this is my Asynctask using RestTemplate

    private class SendLigneVente extends AsyncTask<LigneVente, Void, LigneVente>{

    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected LigneVente doInBackground(LigneVente... params) {
        try {
            HttpHeaders requestHeaders = new HttpHeaders();
            //solution avec Token
            requestHeaders.add("Content-Type","application/json");
            requestHeaders.add("Accept", "application/json");
            requestHeaders.add("Authorization","Bearer "+AuthToken);
            final String url = StaticUrl+"/api/ligne-ventes";
                RestTemplate restTemplate=new RestTemplate();
                restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
                LigneVente lv=new LigneVente();
                HttpEntity entity = new HttpEntity(lv, requestHeaders);
                ResponseEntity<LigneVente> response = restTemplate.exchange(url, HttpMethod.POST,entity,LigneVente.class);
                return response.getBody();
        }catch (Exception e) {
            Log.e("ScanActivity", e.getMessage(),e);

        }
        return null;
    }
    protected void onPostExecute(LigneVente result) {
    }
}

and this is when i call my AsyncTask in a for loop :

public void onClick(View v) {
            new CreateVente().execute();
            for(int j=0;j<ligneVentes.size();j++){
                LigneVente lv=ligneVentes.get(j);
            new SendLigneVente().execute(lv);}}
AmYne Gaïeb
  • 130
  • 3
  • 13
  • what is the problem that you are getting? – nimi0112 Jun 30 '18 at 11:08
  • no error in android debug , nothing happens when i create the new instance of my asynctask in a for loop..However, when i specify an element choosen in my arraylist i can execute my asynctask without a for loop and it works great – AmYne Gaïeb Jun 30 '18 at 11:32

1 Answers1

0

Nothing Is Happenning becausing you are not showing any Feedback in onPostExecute method

protected void onPostExecute(LigneVente result) {
    Toast.maketext(getContext(),"Uploaded 
    ",Toast.DURATION_SHORT).show();
}

EDIT : You are not using the params passed to the AsyncTask

And creating a new object in doInBackground method

LigneVente lv=params[0];
Shrey Gupta
  • 392
  • 4
  • 10
  • I know , and actually i don't need to show any feedback in onPostExecute ! because i said if i try to sent just one element choosen by me in my ArrayList , my code works ! (so no need to show anything in onPostExecute) I just want to loop and send all elements of my list – AmYne Gaïeb Jun 30 '18 at 09:58
  • Technically this should also work Btw how do you know that the request isnt working? – Shrey Gupta Jun 30 '18 at 10:48
  • Mate ,actually i replaced the creation of my new object with param[0] as u mentionned and it works thank you a lot – AmYne Gaïeb Jun 30 '18 at 12:49