Friday, January 27, 2017

How to hide the default “not available” web page from Volley

Leave a Comment

I use Volley library to send json request to a REST service. The json result is then parsed and shown in a webView. It works normally except that it shows a "webpage not available" when it is loading. This page disappears after the data is loaded.

I put a progress dialog during loading but it doesn't hide the default webpage completely. Do you have any idea? Thanks!

public void search(String input) {     showProgressDialog(R.string.loading);     word = RESTTool.encodeForWeb(input);     String url = "HTTP://restserver.com/" + word;     Map<String, String> params = RESTTool.getHeader(word);       JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, new JSONObject(params), new Response.Listener<JSONObject>() {         @Override         public void onResponse(JSONObject response) {             Log.d(TAG, "REST responds successfully");             hideProgressDialog();             String html = convertToHtml(response);             showHtml(html);         }     }, new Response.ErrorListener() {          @Override         public void onErrorResponse(VolleyError error) {             hideProgressDialog();             Log.d(TAG, "receive error");             showHtml(HtmlComposer.getHtmlWithContent(getString(R.string.error_no_network)));         }     }) {         @Override         public Map<String, String> getHeaders() {             Map<String, String> headers = RESTTool.getHeader(word);             return headers;         }      };     RESTClient.getInstance(getContext()).addToRequestQueue(request); } 

2 Answers

Answers 1

The above answer still shows the "webpage not available" page before the custom error page shows.

My solution is to check if the information has been downloaded from the REST api and show my customized error page at last.

The procedure is like this:

  1. Hide the webview before contacting the REST service
  2. In my search(String input) function, download the information from the REST api and check if error happens

    @Override  public void onErrorResponse(VolleyError error) {      hideProgressDialog();     errorHappened = true; } 
  3. Override onPageFinished for WebClient and show the customized webpage if error happens. Because the onPageFinished is called all the time, I put two booleans to make sure that the error page is only loaded once. Otherwise, it can cause problem when the fragment is closed. I put a button below the webview to close the fragment when error happens. It is not shown when no error happens.

      WebViewClient client = new WebViewClient() {     @Override     public void onPageFinished(WebView view, String url) {         if(errorHappened && !errorShown){             webView.loadUrl("file:///android_asset/error.html");             webView.setVisibility(View.VISIBLE);             errorShown = true;             buttonClose.setVisibility(View.VISIBLE);         }     }     //... other code     };    webView.setWebViewClient(client);    // Show the web page    webView.loadDataWithBaseURL("file:///android_asset/", webPage, "text/html", "utf-8", null);    webView.setVisibility(View.VISIBLE); 

Answers 2

I do not know if you still need an answer. Just in case, for anyone who would get here, the solution is rather simple. It should be enough to override the onReceivedError method of your WebViewClient and provide there an error/loading page to override the default one.

For example if you have custom customErrorPage.html page in your asset folder:

yourWebView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {     mWebView.loadUrl("file:///android_asset/customErrorPage.html");     } }); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment