0

It's the first time I use AsyncTask so if the error is very foolish will apologize ...

This is mi class:

class HttpAsyncTask extends AsyncTask<String, Void, String> 
{
    @Override
    protected String doInBackground(String... urls) 
    {
        return null;
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) 
    {       
         Log.i("ASYNC", "size: "+todo.size());
         displayListView();
    }

    public String POST(String url)
    {
        InputStream inputStream = null;
        String result = "";
        try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            //make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
           // pass parameters in this way 

            for(int i=0;i<preguntas.length;i++)
            {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id",String.valueOf(preguntas[i])));
                nameValuePairs.add(new BasicNameValuePair("te",tablas[i]));

                //add data
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);

                // 9. receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // 10. convert inputstream to string
                if(inputStream != null)
                {
                    result = convertInputStreamToString(inputStream);
                    NumPregTem todoAux = new NumPregTem();
                    todoAux.setBBDD(preguntas[i]) ;
                    todoAux.setTema(tablas[i]);

                    String[] aux = result.split(";");   
                    todoAux.setPreg(aux[0]);
                    todoAux.setRespA(aux[1]);
                    todoAux.setRespB(aux[2]);
                    todoAux.setRespC(aux[3]);
                    todoAux.setRespD(aux[4]);
                    todoAux.setRespV(aux[5]);   
                    todo.add(todoAux);
                }
                else
                {
                    result = "Did not work!";
                }

            }
           Log.i("ASYNC", "i've finished to query");
        } 

        catch (Exception e) 
        {
                Log.d("InputStream", e.getLocalizedMessage());
        }
        return result;
    }
}

My problem is that in the debbuger i see the message Log.i("ASYNC", "size: "+todo.size()); in onPostExecute() before the message Log.i("ASYNC", "i've finished to query"); in POST, and the when i use todo.get(i) in my main class (call displayListView() ) this object is null.

thanks you!

i want when the asynchronous task has finished it calls displayListView()

PD: i call POST in this function (in my main class)

HttpAsyncTask httpAsyncTask = new HttpAsyncTask();
httpAsyncTask.execute("http://appdomain.hol.es/webService.php");
mdl
  • 45
  • 8
  • I don't see your POST method getting called anywhere – Droidman Aug 05 '14 at 17:56
  • i've edited the posthttpAsyncTask.execute, in my main class y call the function that call, and when the asynchronous task have finished y want to call displayLstView() – mdl Aug 05 '14 at 18:05
  • what you are doing is passing the parameters to your task. Your POST() method still isn't called. See the answers. Your task don't actually do anything – Droidman Aug 05 '14 at 18:12
  • thanks, the aswer of @SimonSays is right – mdl Aug 05 '14 at 18:20
  • @Droidman sorry to ask this question as a comment, do you can spend some link like putting a bar showing the download status while AsyncTask is running? the bar goes in the method POST? – mdl Aug 05 '14 at 18:28
  • check my related post http://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android/15549639#15549639 – Droidman Aug 05 '14 at 18:45

2 Answers2

2

You probably want to call the POST() method from doInBackground() like this

@Override
protected String doInBackground(String... urls) 
{
    return POST(myUrl);
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • the method post is called in other function, i want that the asynchronous task have finished it calls disPlayListView() – mdl Aug 05 '14 at 18:07
  • you are rigth @SimonSays, thanks, i have wrote this: Override protected String doInBackground(String... urls) { return POST("http://appdomain.hol.es/webService.php"); } Override protected void onPostExecute(String result) { displayListView(); } – mdl Aug 05 '14 at 18:18
  • sorry to ask this question as a comment, do you can spend some link like putting a bar showing the download status while AsyncTask is running? This bar goes in the method POST ? – mdl Aug 05 '14 at 18:26
  • There are many questions here on SO about this topic. Search for "asynctask progress". Here is one example: http://stackoverflow.com/questions/18069678/how-to-use-asynctask-to-display-a-progress-bar-that-counts-down – SimonSays Aug 05 '14 at 18:41
0

Well, cursory observation... The doInBackground() method is the code that is called asynchronously. When that method completes, it calls the onPostExecute() method. Right now, doInBackground() is just returning null, which would immediately call onPostExecute().

I'm not sure when you're calling your POST method, but if you want it to execute before displayListView(), then throw it in the doInBackground() method.

Taylor Frey
  • 374
  • 4
  • 6