title: Trying to parse JSON of a web page before it's loaded in a WebView
body: <p>I need to parse JSON content out of every page in my WebView, before it is shown to the user.</p>
  
  <p>In order to do so, I need to parse the JSON element and only then I’m able to use the <code>shouldOverrideUrlLoading</code> method. I can’t use it before I’ve parsed the JSON object because I rely on that element as part of my implementation of that method.</p>
  
  <hr>
  
  <p>I have created a variable called <code>jsonParsed</code> to indicate whether I have all needed information out of the JSON object. 
  At first, it is initialized to be false.<br>
  When I get a response from the server I know the object was parsed so I change the variable to be true in the <code>onLoaded</code> method, and then the code is ready to get the information and check it. </p>
  
  <hr>
  
  <p>This is the wanted process:</p>
  
  <ol>
  <li>Opening new page by the user</li>
  <li>Entering automatically to <code>shouldOverrideUrlLoading</code> and then to the <code>if</code> statement(JSON wasn't parsed yet) </li>
  <li>Getting the JSON response in <code>OnResponse</code> </li>
  <li>Going back to <code>shouldOverrideUrlLoading</code> and entering the <code>else</code> statement (JSON is parsed)</li>
  </ol>
  
  <p>As you can see, each process should go through both <code>if</code> and <code>else</code> statements, the <code>if</code> before the JSON was parsed, and the <code>else</code> after it was parsed.</p>
  
  <hr>
  
  <p><strong>The problem:</strong></p>
  
  <p><strong><code>shouldOverrideUrlLoading</code> is reached every <em>step 1</em>, but each time it goes into a different statement. The first time goes into the <code>if</code> <em>(steps 2+3)</em>, and the next one goes into the <code>else</code> <em>(step 4)</em> and this process repeats itself...</strong></p>
  
  <p><strong>I think the this line <code>webView.loadUrl(loadingURL);</code> doesn't call <code>shouldOverrideUrlLoading</code> for some reason.</strong></p>
  
  <hr>
  
  <p><strong>Here is my code snippet:</strong>  </p>
  
  <pre><code>    @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon)
      {
       super.onPageStarted(view, url, favicon);
       mProgressDialog.show();                        
      }
  
      @Override
      public boolean shouldOverrideUrlLoading(WebView wView, String url){
                                      loadingURL=url;
              if (!jsonParsed){
              /** JSON object is not parsed
              * Some code for adding request 
  to requestQueue and than loading new url in onResponse method... */                          
              }
  
              else {                         
          /** JSON object is parsed, starting checking process
          * I need to check this URL and decide whether or not to override it*/                               
            jsonParsed=false;
            Root root = new Gson().fromJson(jsonResponse, Root.class);
            for (Page page : root.query.pages.values()) {
            firstParagraph=page.extract; //Parsing process, works fine
                                          }
            if (firstParagraph!=null) {
           //If the webView isn't Wikipedia's article, it will be null
           if (firstParagraph.contains(allowedContent)) {
          //Passing URL to view
           return true;
           }
      }
       return false; //The URL leads to unallowed content  
                                      }
                                  }
  
      @Override
      public void onPageFinished(WebView view, String url) {
       CloseBlocksAndDisableEdit(view);
        mProgressDialog.dismiss();
                           }
  
      private final Response.Listener&lt;String&gt; onLoaded = new Response.Listener&lt;String&gt;() {
      @Override
          public void onResponse(String response) {
           jsonResponse=response; //Getting response
           jsonParsed=true; //Object was examined
           webView.loadUrl(loadingURL);
         }
      };
  </code></pre>
  
  <p><strong>What can be the cause of this problem?<br>
  If it's not possible in this way, I'd like to know which way is better...</strong></p>