brandon66 0 Expert in Progress

Hey guys having some troubles here with screen scraping just wondering whats causing it not to pick up the string from the webpage it throws an error.

package com.lati.latifoodmenu;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

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

        // get http string from URL 
        String strText = DownloadText(
        "http//:www.lakeareatech.edu/current/services/foodservice/index.html");

        // if string is empty, use local address
        if (strText.equals(""))
        {
        strText = DownloadText(
            "http://10.10.0.110/current/services/foodservice/index.html");
        }
     // find starting and ending points in string
        int iStart = strText.indexOf("<h2>&nbsp;</h2>") + 15;
        int iEnd = strText.indexOf("</div>", iStart);

        // get a handle to the web view
        WebView wv = (WebView)findViewById(R.id.webView1);

        // load web view with html
        wv.loadData("<html><body style='color:#ffffff;background-color:#000000;'>" +
            "<h2>LATI Menu Specials</h2>" + 
            strText.substring(iStart, iEnd).replace("<p>", "").replace("\n"," ") + 
            "</body></html>" , "text/html", "UTF-8");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }

    private InputStream OpenHttpConnection(String urlString) throws IOException
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))                     
            throw new IOException("Not an HTTP connection");

        try
        {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect(); 

            response = httpConn.getResponseCode();                 
            if (response == HttpURLConnection.HTTP_OK) 
            {
                in = httpConn.getInputStream();                                 
            }                     
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            throw new IOException("Error connecting" + ex.getMessage());            
        }
        return in;     
    }
    private String DownloadText(String URL)
    {
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try 
        {
            in = OpenHttpConnection(URL);
        } 
        catch (IOException e1) 
        {
            e1.printStackTrace();
            return "";
        }

        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        String str = "";
        char[] inputBuffer = new char[BUFFER_SIZE];          
        try 
        {
            while ((charRead = isr.read(inputBuffer))>0)
            {                    
                //---convert the chars to a String---
                String readString = 
                    String.copyValueOf(inputBuffer, 0, charRead);                    
                str += readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
            return "";
        }    
        return str;        
    }

   }
Be a part of the DaniWeb community

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