Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from web page to string

I'm new to Android and I want to get the whole text from a web page to a string. I found a lot of questions like this but as I said I'm new to Android and I don't know how to use them in my app. I'm getting errors. Only one method I managed to get it to work, it uses WebView, and JavaScript and it is slow as hell. Can someone please tell me some other way to do this or how to speed up the WebView since I don't use it at all for viewing content. By the way, I've added the following code to speed up WebView

webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setBlockNetworkImage(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    webView.getSettings().setPluginsEnabled(false);
    webView.getSettings().setSupportMultipleWindows(false);
    webView.getSettings().setSupportZoom(false);
    webView.getSettings().setSavePassword(false);
    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);
    webView.getSettings().setAppCacheEnabled(false);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

And please if you know other better and faster solution than using WebView please give me the whole source code of main activity or explain where I'm supposed to write it so I don't get errors.

like image 871
null Avatar asked Jan 19 '13 19:01

null


People also ask

How to extract text from HTML?

str = extractHTMLText( code ) parses the HTML code in code and extracts the text.

How do I get only text from innerHTML?

In the browser that supports the standard, you can use textContent instead of innerHTML . Otherwise you can loop through the next nodes and concatenate them, or using library like jQuery that abstract this approach for you. The company is using an older version of IE and textContent returns undefined.

How do I get text content in HTML?

Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.

How do you convert a string in HTML?

The simplest way to do this is to create an element, insert the string into with innerHTML , then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template HTML */ var stringToHTML = function (str) { var dom = document.


1 Answers

This is the code I generally use to download a string from the internet

class RequestTask extends AsyncTask<String, String, String>{

@Override
// username, password, message, mobile
protected String doInBackground(String... url) {
    // constants
    int timeoutSocket = 5000;
    int timeoutConnection = 5000;

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpClient client = new DefaultHttpClient(httpParameters);

    HttpGet httpget = new HttpGet(url[0]);

    try {
        HttpResponse getResponse = client.execute(httpget);
        final int statusCode = getResponse.getStatusLine().getStatusCode();

        if(statusCode != HttpStatus.SC_OK) {
            Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
            return null;
        }

        String line = "";
        StringBuilder total = new StringBuilder();

        HttpEntity getResponseEntity = getResponse.getEntity();

        BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));  

        while((line = reader.readLine()) != null) {
            total.append(line);
        }

        line = total.toString();
        return line;
    } catch (Exception e) {
        Log.w("MyApp", "Download Exception : " + e.toString());
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    // do something with result
}
}

And you can run the task with

new RequestTask().execute("http://www.your-get-url.com/");

like image 119
Ahmed Aeon Axan Avatar answered Oct 02 '22 15:10

Ahmed Aeon Axan