0

I am developing an android app that uses Android Async. I am using this library called Android Asynchronous Http Client

I made a method for a GET request

public String getVenues(String token) throws Exception {
    AsyncHttpClient venuesReq = new AsyncHttpClient();
    venuesReq.addHeader("Authorization", "Token token=" + token);
    venuesReq.get(mainAct.httpRequestURL + "/venues", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            venues = response;
        }
        @Override
        public void onFinish() {
           // Completed the request (either success or failure)
       }
    return venues;
}

but when I call getVenues("token") the return is null, but when I try to call getVenues("token") after few seconds there are now results for venues.

I know that I am using async request so the venues doesn't return immediately.

Now what I want is, when I call getVenues("token") method there should be a returned response for the GET Request.

1 Answer 1

1

You need to use interface here take a look at this

https://stackoverflow.com/a/21773406/472336

Your class from where you are listening/asking for asyntask result need to impliment interface and call that interface method from asyntask..

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

That solution only works if you have a class that extends AsyncTask<String, Void, String> but I already extends my class on a Fragment
No you can impliment this solution with Fragment too. check this link developer.android.com/guide/components/fragments.html public static class FragmentA extends ListFragment { OnArticleSelectedListener mListener; ... @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnArticleSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); } } ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.