Hello, I am developing an android application that sends an sms message to a certain number depending on what is dispayed in the webview. The text in the site that is loaded when the app starts is as follows: YES 0772334556 VOTE ME

Here is part of BrowserActivity.java (excluding sms methods)

......

public class BrowserActivity extends Activity {

    String url = "http://192.241.208.86/zimjobs/zim.php?sn=";
    static String serial = null;


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
  }



    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);

        webview=(WebView)findViewById(R.id.webview1);
        webview.setWebViewClient(new MyWebViewClient());
        openURL();

        // if statement, sms should be sent if YES
        if(getData() == true){
        for (int i=0; i < 3; i++)
        { 
        Toast.makeText(getApplicationContext(), 
                url + getDeviceId(), Toast.LENGTH_LONG).show();
        }

        // SMS method goes here. SMS is sent

        }


        else{

            for (int i=0; i < 3; i++)
            { 
            Toast.makeText(getApplicationContext(), 
                    "SMS not Sent", Toast.LENGTH_LONG).show();

                    // SMS not sent
            }
        }

    }

     /** Opens the URL in a browser */
    private void openURL() {
        webview.loadUrl(url + getDeviceId());
        webview.requestFocus();

    }


    /** Reads content from web page 
     * @return */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

    public boolean getData(){ // Method not working

        String text = "YES";


        webview.findAll(text);
        webview.findAllAsync(text);


        return true;

    }


    // Get device serial number
    public static String getDeviceId() {

        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception ignored) {
        }

        return serial;
    }

    .....

Now in the above code I have included the webview.findAll(text) and webview.findAllAsync(text) in getData() and used it in the onCreate method to find the "YES" string in page loaded in webview but it seems not to work.

All I want to do is to get certain text from the webview and maybe store the string in the code (using arrays, json or something else), then assign the string to a variable. For example I want to get the YES text(as well 0772334556 and VOTE ME) from the webview, store it in a variable and then use the variable in an if statement to determine whether the sms should be sent or not.

I have looked into using HTTPClient methods and javascript to get the html source code as well as create an AsyncTask class but they all look messy. I have included INTERNET and READ_PHONE_STATE permissions in the android manifest for internet and serial number access respectively, as well as WebView in activity_browser.xml.

Any help would be greatly appreciated.

Ah, findall is used to highlight words in the webview, and it's rather buggy.

What you really should do is to make a HttpClient call to the webpage to retrieve the page into memory. You can then access the code and also display it in the webview, all with a single http call.

The following code snippet, when executed in a background thread, will place the retreived HTML source code into a StringBuffer for you. You can then parse it from there like a normal String:

    public static void showHTML(String url) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;

        try {
            response = httpclient.execute(httpget);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(instream));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        instream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                Log.e("HTML", sb.toString());

                instream.close();
            }
        } catch (Exception e) {
            Log.e("ERROR", e.getMessage(), e);
        }
    }

You may consider using JSOUP library for parsing html. Better then doing "by hand"

Well I have decided to write my web page content in JSON format like so:

nameofpage.php:

{
"condition":"YES", 
"phone":"0772334556", 
"msg":"Testing"
}

Now what i want to do is use JSON Parser to get JSON from the url and store each JSON item in a variable, then use variables in my code. Looks simpler to me. How do I do it?

You can use either Android provided JSON capabilities (little cruel and long winded) or you can use any of number of open source libraries like Gson, Jackson that let you easily parse json to specific class

StringBuilder sb = new StringBuilder("<html><body>");
sb.append(readTextFile());

StringBuilder sb = new StringBuilder("<html><body>");
sb.append(readTextFile());
sb.append("</body></html>");
myWebView.loadData(sb.ToString(), "text/html", "UTF-8");

public String readTextFile(String filename) {
    // Open and read the contents of <filename> into
    // a single string then return it
}

@anas.man you may misunderstand question

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.