Asynchronous Call to a Restfull Webservice

Asked

Viewed 668 times

-1

I need to make a call to Webservice on Android, and with another class call it. At the end show on design the answer obtained by Ws. I already made the Webservice only that the part of "asynchronous" is not giving.

This is my Webservice, 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 side of "Android Activity" I call this class asynchronous 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 it is not actually making an asynchronous call and the results are almost always not received by Webservice.

Is there any simpler way to do this or am I doing it wrong somewhere like the Webservice call or the Webservice class itself?

  • A good example to start this one. http://programmerguru.com/android-tutorial/android-restful-webservice-tutorial-part-1/

  • The answer is passed to the method onPostExecute() through the parameter result. It is in this method that you must treat the answer.

  • You are doing nothing in the code above, you have deleted the code on doInBackground ? As it stands, you just display a dialog and close

2 answers

0

I am on a similar project. WS will return the data to its main Activity.

The way to solve by Assynctask that really does what you want, rather than waiting for the thread’s time and rather do what will happen next. look at the answers to that question about using Thread or assync or Handler https://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread

Here’s an example of assynctask https://jsfiddle.net/nzqLxa5v/ But this one’s from my project, so you’ll adapt. But what happens there is you understand doInBackground and onPostExecute

  • Instead of placing a link, place the code directly in the answer.

  • I put the link to not pollute the topic and not waste time giving spaces since the intention is only to help

  • The code is not pollution, the vast majority of responses have associated code and it should be posted. To format the code you do not need to enter spaces line by line, just select it and click Ctrl + k.

  • 1

    I didn’t know this, I’m new here yet. I didn’t know about Ctrl k. Thanks.

0

Miguel, I’m no expert on Java, but considering the concept of Threads you are using "Thread.Sleep(1000)" for what reason?

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

1) Activity is synchronous by exactly 1 second with the asynchronous class this is not the correct way to deal with the return of an asynchronous class.

2) No code has been presented for retrieval of results in your question, so I start from the understanding that you are doing this right after Thread.Sleep, which will not always be enough.

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

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

I hope I helped, buddy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.