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:)
Code :
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> </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;
}
}
Re: android screen scraping error
Quote:
it throws an error
Can you post the full text of the error message, showing Where and what happened.
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
Code :
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
Re: android screen scraping error
This code works for android 2.3.3 but not the new version of android any help?
Re: android screen scraping error
Hey dude, please help him, I am newbie here.