Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: android screen scraping error

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default android screen scraping error

    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. any help would be appreciated thanks
    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;        
        }
       }
    Last edited by Norm; December 11th, 2012 at 05:20 PM. Reason: Moved to Android section


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: android screen scraping error

    it throws an error
    Can you post the full text of the error message, showing Where and what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: android screen scraping error

    This is the logcat i believe its not getting anything from the webpage not sure how to fix that. Caused by: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=14; regionLength=-15
    12-11 22:27:42.183: E/Trace(877): error opening trace file: No such file or directory (2)
    12-11 22:27:43.303: E/AndroidRuntime(877): FATAL EXCEPTION: main
    12-11 22:27:43.303: E/AndroidRuntime(877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lati.latifoodmenu/com.lati.latifoodmenu.MainActivity}: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=14; regionLength=-15
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.os.Looper.loop(Looper.java:137)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at java.lang.reflect.Method.invokeNative(Native Method)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at java.lang.reflect.Method.invoke(Method.java:511)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at dalvik.system.NativeStart.main(Native Method)
    12-11 22:27:43.303: E/AndroidRuntime(877): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=14; regionLength=-15
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at java.lang.String.startEndAndLength(String.java:583)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at java.lang.String.substring(String.java:1464)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at com.lati.latifoodmenu.MainActivity.onCreate(MainActivity.java:42)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.Activity.performCreate(Activity.java:5104)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    12-11 22:27:43.303: E/AndroidRuntime(877): 	... 11 more

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: android screen scraping error

    This code works for android 2.3.3 but not the new version of android any help?

  5. #5

    Default Re: android screen scraping error

    Hey dude, please help him, I am newbie here.

Similar Threads

  1. Title screen Glitch (Cant Bust ERROR)
    By ErrorBuster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 3rd, 2012, 02:45 PM
  2. startActivity error in for Android graph view
    By bdtuhin007 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 16th, 2012, 09:29 PM
  3. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  4. error when switch activity in android
    By yefta in forum Android Development
    Replies: 3
    Last Post: October 26th, 2011, 10:50 AM