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