I have a problem with the simplest email client from my Android app to Google app.
So here is the error message:
07-16 15:58:02.314: E/AndroidRuntime(911): FATAL EXCEPTION: main
07-16 15:58:02.314: E/AndroidRuntime(911): java.lang.IllegalStateException: Could not execute method of the activity
This happens in my Android application for the httpclient.execute command:
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8888/mywebapp");
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
There is no trace on my server that I come to my post method:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SYL: POST request received");
super.doPost(req, resp);
}
Server is a google web application running under my eclipse locally using this conf:
<servlet-name>MyWebApp</servlet-name>
<servlet-class>com.my.mywebapp.MyWebAppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyWebApp</servlet-name>
<url-pattern>/mywebapp/*</url-pattern>
</servlet-mapping>
If the get test method, which I call through the web browser, it works. But the get call in java (just like I do for the message) does not work. As in the Android application, the connection cannot be created with my server.
I set these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can someone help me?
Thank.
PS:
Still the same error when replacing localhost with 10.0.2.2. I copy the full trace:
07-16 18:13:34.945: E/AndroidRuntime(1081): FATAL EXCEPTION: main
07-16 18:13:34.945: E/AndroidRuntime(1081): java.lang.IllegalStateException: Could not execute method of the activity
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.view.View$1.onClick(View.java:3044)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.view.View.performClick(View.java:3511)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.view.View$PerformClick.run(View.java:14105)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.os.Handler.handleCallback(Handler.java:605)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:137)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 18:13:34.945: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 18:13:34.945: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 18:13:34.945: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method)
07-16 18:13:34.945: E/AndroidRuntime(1081): Caused by: java.lang.reflect.InvocationTargetException
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.view.View$1.onClick(View.java:3039)
07-16 18:13:34.945: E/AndroidRuntime(1081): ... 11 more
07-16 18:13:34.945: E/AndroidRuntime(1081): Caused by: android.os.NetworkOnMainThreadException
07-16 18:13:34.945: E/AndroidRuntime(1081): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-16 18:13:34.945: E/AndroidRuntime(1081): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-16 18:13:34.945: E/AndroidRuntime(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-16 18:13:34.945: E/AndroidRuntime(1081): at com.my.my.MyActivity.sendMessage(MyActivity.java:72)
07-16 18:13:34.945: E/AndroidRuntime(1081): ... 14 more