Kyle
Kyle

Reputation: 607

Android: Having trouble getting html from webpage

I'm writing an android application that is supposed to get the html from a php page and use the parsed data from thepage. I've searched for this issue on here, and ended up using some code from an example another poster put up. Here is my code so far:

HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    try {
        Log.d("first","first");
        HttpResponse response = client.execute(request);
        String html = "";
        Log.d("second","second");
        InputStream in = response.getEntity().getContent();
        Log.d("third","third");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        Log.d("fourth","fourth");
        StringBuilder str = new StringBuilder();
        String line = null;
        Log.d("fifth","fifth");
        while((line = reader.readLine()) != null) {
            Log.d("request line",line);
        }
        in.close();
    } catch (ClientProtocolException e) {

    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("error", "error");
    }
    Log.d("end","end");
}

Like I said before, the url is a php page. Whenever I run this code, it prints out the first first message, but then prints out the error error message and then finally the end end message. I've tried modifying the headers, but I've had no luck with it. Any help would be greatly appreciated as I don't know what I'm doing wrong.

Thanks!

When I do e.getMessage() and print that out, in the logger all it says is stanford.edu. Hope that helps. Here is the stack trace:

01-17 16:58:27.687: WARN/System.err(452): java.net.UnknownHostException: stanford.edu 01-17 16:58:27.947: WARN/System.err(452): at java.net.InetAddress.lookupHostByName(InetAddress.java:506) 01-17 16:58:27.947: WARN/System.err(452): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 01-17 16:58:27.977: WARN/System.err(452): at java.net.InetAddress.getAllByName(InetAddress.java:256) 01-17 16:58:27.977: WARN/System.err(452): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136) 01-17 16:58:28.027: WARN/System.err(452): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 01-17 16:58:28.027: WARN/System.err(452): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 01-17 16:58:28.047: WARN/System.err(452): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348) 01-17 16:58:28.057: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 01-17 16:58:28.057: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 01-17 16:58:28.117: WARN/System.err(452): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 01-17 16:58:28.117: WARN/System.err(452): at edu.stanford.cs247.util.ColorHandler.getInfo(ColorHandler.java:44) 01-17 16:58:28.217: WARN/System.err(452): at edu.stanford.cs247.util.ColorHandler.handleMessage(ColorHandler.java:70) 01-17 16:58:28.217: WARN/System.err(452): at android.os.Handler.dispatchMessage(Handler.java:99) 01-17 16:58:28.247: WARN/System.err(452): at android.os.Looper.loop(Looper.java:123) 01-17 16:58:28.257: WARN/System.err(452): at android.app.ActivityThread.main(ActivityThread.java:3647) 01-17 16:58:28.257: WARN/System.err(452): at java.lang.reflect.Method.invokeNative(Native Method) 01-17 16:58:28.297: WARN/System.err(452): at java.lang.reflect.Method.invoke(Method.java:507) 01-17 16:58:28.297: WARN/System.err(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-17 16:58:28.327: WARN/System.err(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-17 16:58:28.337: WARN/System.err(452): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 377

Answers (4)

user2677537
user2677537

Reputation: 1

add to Manifest <uses-permission android:name="android.permission.INTERNET" />

Upvotes: 0

Kyle
Kyle

Reputation: 607

I forgot to add the internet permission in my manifest file.

Upvotes: 0

Cheryl Simon
Cheryl Simon

Reputation: 46844

The first line of your stacktrace says:

java.net.UnknownHostException: stanford.edu

You should learn to read stacktraces. A quick search for "UnknownHostException" tells you it is:

Thrown to indicate that the IP address of a host could not be determined.

This means that it can't turn stanford.edu into an IP. You probably need www.stanford.edu or something like that. Try pinging the address on the command line ping www.stanford.edu , and make sure it resolves to an ip address.

Upvotes: 0

Bruno Berisso
Bruno Berisso

Reputation: 1091

First of all, the Log.d() method calls may be something like Log.d("MyAppName", "Message") not Log.d("Message", "Message") In this way you can create a filter in LogCat for your app and see only your messages. (Check this on Eclipse)

For your problem, try putting some useful message in the catch block, something like:

try{
...
} catch (IOException e){
Log.d("MyAppName", e.getMessage());
}

This will show you the problem that throw the exception. Consider take a look to this sample code, the getUrlContent() do exactly what you wont.

Good luck

Upvotes: 1

Related Questions