Asynchronous call to a RestFull WebService

-1

I need to make a call to the WebService on Android, and with another class call it. At the end show in design the response obtained by ws. I've already done the WebService but that part of the "asynchronous" is not giving.

This is my WebService, you get three strings :

public class WebServiceRestFull extends AsyncTask<String, String, String>
{
    protected ProgressDialog dialog;


    public String wsURL;
    public String wsFunction;
    public String wsInput;

    public int codigoHTTP;
    public String mensagemHTTP;
    public String strResposta;


    public WebServiceRestFull(Context act)
    {
        dialog = new ProgressDialog(act);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Aguarde por favor...");
        dialog.show();
    }

    @Override
    protected void onPostExecute(String result)
    {
        if (dialog.isShowing())
        {
            dialog.dismiss();
        }
    }

    @Override
    protected String doInBackground(String... params)
    {
        //chamada ao webservice e retorna uma string

        //return resposta do webservice;
    }      
}

On the "Android Activity" side I call this asynchronous class as follows:

WebServiceRestFull web = new WebServiceRestFull(this);
web.wsURL = "http://someurl.com/rest/etc";
web.wsFunction = "login";
web.wsInput = "mike";
web.execute();
Thread.sleep(1000);

The problem is that you are not actually doing an asynchronous call and the results are almost always not received by the WebService.

Is there any simpler way to do this or am I doing wrong on some side such as calling the WebService or the WebService class itself?

    
asked by anonymous 05.07.2016 / 13:35

2 answers

0

Miguel, I'm not a Java expert, but considering the concept of Threads you're using "Thread.sleep (1000)" for what reason?

If your class is making an asynchronous request and you are waiting a second for it, then it probably is not implemented as it should, for the following reasons:

1) Activity is synchronous for exactly 1 second with the asynchronous class This is not the correct way to handle the return of an asynchronous class.

2) No code has been presented for retrieving results in your question, so I understand that you are doing this right after Thread.sleep, which will not always be enough.

So I recommend that you implement some way (events?) to detect when your asynchronous class completes the search of information in WS and then retrieve the desired value, so that this does not prevent the continuous execution of Activity (this would be asynchronous) .

I will not put any examples in code because my contribution in Java is limited to theory applied to other languages as well.

I hope I have helped, my friend.

    
05.07.2016 / 13:46
0

I'm on a similar project. WS will return the data to your main activity.

The way to solve by AssyncTask which really does what you want, rather than wait for thread time and yes do what will happen next. look at the answers to this question about using Thread or assync or handler link

Here's an example of assynctask link but this is my project, there you go to adapt. But what happens there is you understand the doInBackground and onPostExecute

    
08.07.2016 / 17:27