0

I'm trying to do implement login using a ASP.Net Web Api into an Android application.
What I have so far are functions that work, just that I want to make the login request kind of synchronous instead of asynchronous. I'm using Android Asynchronous Http Client like they say on their website.

public class ApiInterface {
    public static final String ApiURL = "http://******/api/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get4Login(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return ApiURL + relativeUrl;
    }
}

And I have this function in LoginActivity:

private boolean doLogIn(String user, String pass) {
    boolean result = false;

    if (user.trim().isEmpty() || pass.trim().isEmpty()) {
        return false;
    }
    RequestParams params = new RequestParams();
    params.add("user", user);
    params.add("pass", pass);

    ApiInterface.get4Login("Auth", params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable error) {
            Toast.makeText(MyApp.getContext(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            //***Here I want to set the doLogIn() function result depending on the response from the server;***
            Toast.makeText(MyApp.getContext(), "Lista sesizari incarcata!", Toast.LENGTH_LONG).show();
        }
    });

    return result;
}

Is there any way to do this?

Alin I
  • 580
  • 1
  • 7
  • 24

1 Answers1

0

On your MyTextHttpResponseHandler class should define a variable named result and set type is boolean,default to false,then define a method to get the result value,like public boolean getResult(){return this.result;} then you can change the result value on onSuccess and onFailure method. Next your doLogIn method will like this

private boolean doLogIn(String user, String pass) {
//boolean result = false;

if (user.trim().isEmpty() || pass.trim().isEmpty()) {
    return false;
}
RequestParams params = new RequestParams();
params.add("user", user);
params.add("pass", pass);
MyTextHttpResponseHandler myTextHttpResponseHandler = new MyTextHttpResponseHandler(this);
ApiInterface.get4Login("Auth", params, myTextHttpResponseHandler);
return myTextHttpResponseHandler.getResult();
}
starkshang
  • 8,228
  • 6
  • 41
  • 52
  • the **return myTextHttpResponseHandler.getResult();** is executed before the onSuccess method. – Alin I Sep 30 '15 at 13:59
  • oh,yes, but I don't suggest you use synchronous method to post a network request(you konw the reason),since you want to know the result of the request,maybe you want the result to invoke a method called `A(boolean)`,so to achieve the goal,you can let `doLogIn` to return void,and when your textHttpResponseHandler invoked `onSuccess ` or `onFailure` invoke `A(boolean)` with the result(if invoke `onSuccess ` then call `A(true)`,else if `onFailure` call `A(false)`) – starkshang Sep 30 '15 at 15:17