0

I am writing an android application where I make a request through volley and load that into the web view. But now when I connect to a public wifi (say Starbucks or some hotel) where the user first needs to signin, when i make a request the login page is returned where the user puts his credentials (email, password or whatever) and that is loaded into the web view. But i need to detect that the wifi first needs to signin and show user a message to login through browser

Harsh Shah
  • 368
  • 3
  • 17

1 Answers1

1

In such a case, the Captive portal is redirecting you to the login page. Base on the valley configuraions, it may or may not get an error. (See this). So you can disable volley redirection, and in onErrorResponse(VolleyError error) callBack method, check if you getting error code 302, then you have to open the browser for user or whatever you want to do to handle this case.

To check for response code do this:

@Override
public void onErrorResponse(VolleyError error) {
    NetworkResponse networkResponse = error.networkResponse;
    if (networkResponse != null && networkResponse.statusCode == 302) {
        // Open the browser
    }
}

EDIT: If you get any error, you can simple check the error and decide whether open a browser for user or not. But if you getting the result in onRespose and not getting a 302 as an error, you should check for response code and use the following code instead to be able to check the response code:

queue.add(new Request<String>(Method.GET, url, errorListener) {

  @Override
  protected Response<String> parseNetworkResponse(NetworkResponse response) {
      if (response != null && reponse.statusCod == 302){
           // Open the browser for user, or whatever you want to do
      }
      return null;
  }

  @Override
  protected void deliverResponse(String response) {

  }});

Also Check here too

Community
  • 1
  • 1
Sadegh
  • 2,669
  • 1
  • 23
  • 26
  • Would this mean I would have to modify the volley code to achieve this? I am not allowed to modify code in it (for now). – Harsh Shah Aug 13 '14 at 04:38
  • No, when you are setting your request listeners, you have to override the `onErrorResponse` method there, have a look on [this example](http://java.dzone.com/articles/android-%E2%80%93-volley-library) that shows you how to use volley. – Sadegh Aug 13 '14 at 04:58
  • This works with 'http' but when i make an 'https' request it returns with a 'com.android.volley.NoConnectionError: java.io.IOException: Hostname 'google.com' was not verified' – Harsh Shah Aug 13 '14 at 18:38
  • So you have to handle this exception too, basically you are getting an exception since the result is not from real google! so in such cases you should open the browser too. – Sadegh Aug 13 '14 at 18:44
  • Yes i can do that, but then can't this exception be because of other reasons? – Harsh Shah Aug 13 '14 at 18:54
  • Added some code in case you are not getting `onErrorReponse` callback, and what to check for response status code in result. – Sadegh Aug 13 '14 at 19:10