0

I am trying to load a webview using a search term. So, say the searchterm entered in the search box is "abcnews" it will load the url (http://server.com/something/"+searchterm+"%20something) as (http://server.com/something/abcnews/something) But suppose if the searchterm doesnt exist, say I load something like "whatever", for which http://server.com/something/whatever/something is invalid I want the page to redirect to another page within my app (say relatedsearch view). How do I go about the same? Here's my code for the same so far: In searchtermfragment:

     if((searchTerm.contains(mSearchView.getText().toString().toLowerCase() + " " + mSearchView.getText().toString().toLowerCase())))){

             WebViewFragment frag = WebViewFragment.newInstanceForSearch(getFragmentManager(), searchTerm, null);
             frag.setSearchTerms(searchTerm);

    }
    else 
    {
             RelatedSearchResultsFragment.newInstance(getFragmentManager(), searchTerm);
    }
 }

and in the search webview class I have:

if (environmentApi.equalsIgnoreCase(NetworkUtils.Apis.first)) {

            url = "https://mobile13.com/ms.0/fwd/service/v1/?q="+SearchTerm+"%20revenue&ui.theme=novadark&userAgent=android";

        }

and in the oncreateview I have set the webview.loadurl(url); I am not sure where or how do I validate the searchterm to get the respective fragment accordingly if the searchterm is invalid( as the url doesnt switch either upon entering an invalid search term).Any clue on how to do the same?

Hariharan
  • 24,741
  • 6
  • 50
  • 54

1 Answers1

0

I would like to suggest you to grab the html response when you search, then parse the result, if it returns correct value (search term is valid), go to webview, if it is invalid (search term not found, related searches), go to other activity.

You might want to use httpURLconnection or other method to get the html response.

Here's the algorithm :

  1. User enters search term
  2. OnClick/Confirm, check the URL validity
  3. To check the validity, first you need to grab the html response from URL
  4. Next, Parse the result, search for values that shows it is a "valid" search
  5. If it is valid, go to webview, if it is invalid, go to other view.

Hope this helps, Good Luck

Reid

reidzeibel
  • 1,622
  • 1
  • 19
  • 24
  • can you explain programmatically, i have no clue how to go about it –  Sep 27 '13 at 15:44
  • This is to get HTTP Response (Step 3) : http://stackoverflow.com/questions/5769717/how-can-i-get-an-http-response-body-as-a-string-in-java To parse the response (step 4) : http://stackoverflow.com/questions/7830972/jsoup-parsing-html if valid, call the url in webview, if invalid, go to other view – reidzeibel Sep 30 '13 at 02:30