0

I'm having a hard time identifying what the problem is in my code. I'm stuck on this part and the debugger is somehow cryptic. Please see enclosed code: The manifest file has the

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

Next is the code for the start, I've marked the two try/catch exceptions with a system.out "Exception 1" and "Exception 2" to quicky identify in the logcat

package com.example.httpexample;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class HttpExample extends Activity {

TextView httpStuff;


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

    httpStuff = (TextView) findViewById(R.id.tvHttp);
    GetMethodEx test = new GetMethodEx();
    String returned;
    try {
        returned = test.getInternetData();
        httpStuff.setText(returned);
    } catch (Exception e) {
        System.out.println("Exception 1");
        e.printStackTrace();
    }
  }
}

the second block of code is the actual http class

package com.example.httpexample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetMethodEx {

public String getInternetData() throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://google.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) !=null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    } finally {
        if (in != null){
            try{
                in.close();
                return data;
            } catch (Exception e) {
                System.out.println("Exception 2");
                e.printStackTrace();
            }
        }
    }
  }
}

My problem is that I can't identify what the problem is using the debugger am I looking in the wrong place, is there more information there??? Here is a little of the logcat output that I get right after the exception, but again I don't know hot to interpret it or if there is more information somewhere else, Thanks in advance for the help!

07-19 21:37:30.916: I/System.out(4987): Exception 1
07-19 21:37:30.916: W/System.err(4987): android.os.NetworkOnMainThreadException
07-19 21:37:30.936: W/System.err(4987):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-19 21:37:30.936: W/System.err(4987):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-19 21:37:30.936: W/System.err(4987):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-19 21:37:30.947: W/System.err(4987):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-19 21:37:30.947: W/System.err(4987):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-19 21:37:30.956: W/System.err(4987):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-19 21:37:30.966: W/System.err(4987):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-19 21:37:30.966: W/System.err(4987):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-19 21:37:30.966: W/System.err(4987):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-19 21:37:30.976: W/System.err(4987):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-19 21:37:30.976: W/System.err(4987):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-19 21:37:30.986: W/System.err(4987):     at com.example.httpexample.GetMethodEx.getInternetData(GetMethodEx.java:22)
07-19 21:37:30.986: W/System.err(4987):     at com.example.httpexample.HttpExample.onCreate(HttpExample.java:21)
07-19 21:37:30.996: W/System.err(4987):     at android.app.Activity.performCreate(Activity.java:5104)
07-19 21:37:30.996: W/System.err(4987):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
..
..
.. and more
user2566468
  • 179
  • 2
  • 11

1 Answers1

0

Here is an article about the exception you're getting.

You can also add Log.d(String, String) lines to your code, to follow each step and see exactly where you're getting stuck. The first argument is the tag, the second is a string you define which LogCat will spit back at you when it hits that line of code.

It's extremely helpful. I'll bet the article has your answer though.

Edit to add the developer doc, looks like running the Network action in your Main thread is the culprit.

Edit #2 for the fix: TechBlogIsTech Article

Edit #3 for more on AsyncTask from developer docs.

Final Edit for more on LogCat debug log writing.

Community
  • 1
  • 1
Phoenix
  • 1,881
  • 5
  • 20
  • 28
  • You were right, its related to the StrictMode that forces you not to use the main thread for extra tasks, thanks. – user2566468 Jul 20 '13 at 07:13