Search code examples
androidsdkbufferedreaderurlconnection

BufferReader Android


Hello i'm trying to set a simple connection to download the webpage source through an android application so i could later on use it in conjunction with my api. My code bellow show's no errors but in run time the application quits when this code is run. When i comment out the BufferReader line the application continues as normal so the problem must be with this line of code:

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

And bellow i've got the full code snippet from where i'm using it.

        currentCustomer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                URL url = null;
                url = new URL("http:www.google.com");
                URLConnection conn = url.openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line = "";
                while((line = reader.readLine()) != null) {
                    tv.append(line);
                }
            } catch(Exception e) {

            }

        }
    }); 

Any help would be appreciated :)

And Here's the stack trace:

06-15 16:15:50.911: D/dalvikvm(925): GC_FOR_ALLOC freed <1K, 4% free 7249K/7495K, paused 59ms
06-15 16:15:51.049: D/dalvikvm(925): GC_CONCURRENT freed <1K, 4% free 7249K/7495K, paused 5ms+4ms
06-15 16:15:51.159: V/TLINE(925): new: android.text.TextLine@40633f50
06-15 16:15:51.489: V/TLINE(925): new: android.text.TextLine@4063be20
06-15 16:15:55.019: W/System.err(925): java.lang.NullPointerException
06-15 16:15:55.039: W/System.err(925):  at      org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.hashCode(HttpConnection.java:296)
06-15 16:15:55.039: W/System.err(925):  at java.util.HashMap.get(HashMap.java:302)
06-15 16:15:55.039: W/System.err(925):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:73)
06-15 16:15:55.039: W/System.err(925):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
06-15 16:15:55.049: W/System.err(925):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
06-15 16:15:55.049: W/System.err(925):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
06-15 16:15:55.049: W/System.err(925):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
06-15 16:15:55.049: W/System.err(925):  at com.kylemcbride.SqueakyClean.SqueakyCleanActivity$1.onClick(SqueakyCleanActivity.java:48)
06-15 16:15:55.049: W/System.err(925):  at android.view.View.performClick(View.java:3100)
06-15 16:15:55.049: W/System.err(925):  at android.view.View$PerformClick.run(View.java:11644)
06-15 16:15:55.059: W/System.err(925):  at android.os.Handler.handleCallback(Handler.java:587)
06-15 16:15:55.059: W/System.err(925):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 16:15:55.069: W/System.err(925):  at android.os.Looper.loop(Looper.java:126)
06-15 16:15:55.069: W/System.err(925):  at android.app.ActivityThread.main(ActivityThread.java:3997)
06-15 16:15:55.069: W/System.err(925):  at java.lang.reflect.Method.invokeNative(Native Method)
06-15 16:15:55.069: W/System.err(925):  at java.lang.reflect.Method.invoke(Method.java:491)
06-15 16:15:55.080: W/System.err(925):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-15 16:15:55.080: W/System.err(925):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-15 16:15:55.080: W/System.err(925):  at dalvik.system.NativeStart.main(Native Method)

Solution

  • You're trying to load the url:

    http:www.google.com
    

    Note the lack of slashes. It's not a real address.