I am trying to relate the rest api to get a JSON document, but when I run the application in the Eclipse Android emulator, the application closes immediately after it starts ("Unfortunately, TestRest has stopped"). It seems to close due to a call to the httpClient.execute method.
The following is the main action code:
public class TestRestActivity extends Activity {
TextView info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
info = (TextView) findViewById(R.id.debug);
String testUri = "http://www.entireweb.com/xmlquery?pz=MYAPIKEY&ip=IP&q=pizza&n=50&format=json";
info.setText(testUri);
HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet(testUri);
HttpResponse response = null;
try {
response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
info.setText("got it");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
I also included web permissions in the manifest file, as shown below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.john.testRes"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
Not too familiar with Android development and really clueless about what causes the problem. I use api and it returns the expected json document from my browser. The AVD that I use can connect to the Internet in my browser, and I tried the application on several different AVDs.
Any suggestions as to what causes the problem? Thank.
:
11-22 18:13:14.221: D/AndroidRuntime(556): Shutting down VM
11-22 18:13:14.221: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
11-22 18:13:14.251: E/AndroidRuntime(556): FATAL EXCEPTION: main
11-22 18:13:14.251: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.john.testRes/org.john.testRes.TestRestActivity}: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.Looper.loop(Looper.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 18:13:14.251: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-22 18:13:14.251: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-22 18:13:14.251: E/AndroidRuntime(556): at dalvik.system.NativeStart.main(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556): Caused by: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.john.testRes.TestRestActivity.onCreate(TestRestActivity.java:41)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.Activity.performCreate(Activity.java:4465)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-22 18:13:14.251: E/AndroidRuntime(556): ... 11 more
11-22 18:13:48.321: I/Process(556): Sending signal. PID: 556 SIG: 9
11-22 18:14:01.552: D/AndroidRuntime(597): Shutting down VM