Android应用程序的doPost [英] Android Application doPost
问题描述
我有一个问题,这样做从我的Android应用一个简单的HTTP POST请求到我的谷歌应用程序。
因此,这里的错误消息:
15 07-16:58:02.314:E / AndroidRuntime(911):致命异常:主要
07-16 15:58:02.314:E / AndroidRuntime(911):java.lang.IllegalStateException:无法执行活动的方法
这在httpclient.execute命令发生在我的Android应用:
ConnectivityManager connMgr =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
的NetworkInfo NETWORKINFO = connMgr.getActiveNetworkInfo();
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(HTTP://本地主机:8888 / mywebapp);
尝试{
//执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost);
}
在我的服务器没有任何痕迹,我在我的帖子的方法得出:
保护无效的doPost(HttpServletRequest的REQ,HttpServletResponse的RESP)抛出了ServletException,IOException异常{
的System.out.println(SYL:POST请求收到);
super.doPost(REQ,RESP);
}
服务器与此的conf我的日食下运行谷歌本地应用韦伯:
< servlet的名称>&MyWebApp LT; / servlet的名称>
<的servlet类> com.my.mywebapp.MyWebAppServlet< / servlet的类>
< / servlet的>
< Servlet映射>
< servlet的名称>&MyWebApp LT; / servlet的名称>
< URL模式> / mywebapp / *< / URL模式>
< / Servlet映射>
如果测试get方法,我通过一个Web浏览器工作原理调用。
但调用Java中的GET(我这个职位做同样的方式)不起作用。
它喜欢在Android应用程序的连接不能与我的服务器做。
我把这些权限:
<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>
有人可以帮我请??
感谢您。
PS:
不过同样的错误与10.0.2.2进行更换本地主机。
我复制完整的跟踪:
07-16 18:13:34.945:E / AndroidRuntime(1081):致命异常:主要
07-16 18:13:34.945:E / AndroidRuntime(1081):java.lang.IllegalStateException:无法执行活动的方法
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.view.View $ 1.onClick(View.java:3044)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.view.View.performClick(View.java:3511)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.view.View $ PerformClick.run(View.java:14105)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.os.Handler.handleCallback(Handler.java:605)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.os.Handler.dispatchMessage(Handler.java:92)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.os.Looper.loop(Looper.java:137)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.app.ActivityThread.main(ActivityThread.java:4424)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.lang.reflect.Method.invokeNative(本机方法)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.lang.reflect.Method.invoke(Method.java:511)
07-16 18:13:34.945:E / AndroidRuntime(1081):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
07-16 18:13:34.945:E / AndroidRuntime(1081):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-16 18:13:34.945:E / AndroidRuntime(1081):在dalvik.system.NativeStart.main(本机方法)
07-16 18:13:34.945:E / AndroidRuntime(1081):因:java.lang.reflect.InvocationTargetException
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.lang.reflect.Method.invokeNative(本机方法)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.lang.reflect.Method.invoke(Method.java:511)
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.view.View $ 1.onClick(View.java:3039)
07-16 18:13:34.945:E / AndroidRuntime(1081):11 ...更多
07-16 18:13:34.945:E / AndroidRuntime(1081):因:android.os.NetworkOnMainThreadException
07-16 18:13:34.945:E / AndroidRuntime(1081):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-16 18:13:34.945:E / AndroidRuntime(1081):在java.net.InetAddress.getAllByName(InetAddress.java:220)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-16 18:13:34.945:E / AndroidRuntime(1081):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-16 18:13:34.945:E / AndroidRuntime(1081):在com.my.my.MyActivity.sendMessage(MyActivity.java:72)
07-16 18:13:34.945:E / AndroidRuntime(1081):14 ...更多
如果你是在本地服务器调试,更改您要访问的IP,并设置这一项:10.0.2.2。
所以,你的httpost看起来是这样的:
HttpPost httppost =新HttpPost(http://10.0.2.2:8888/mywebapp);
有关更多信息,请 http://developer.android.com/tools/设备/ emulator.html#networkaddresses
I am having a problem doing a simple http post request from my android app to my Google Application.
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
It happens in my android app at 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);
}
On my server there is no trace that I arrive in my post method:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SYL: POST request received");
super.doPost(req, resp);
}
The server is google webb app running under my eclipse locally with 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 a test a get method that I call through a web navigator it works. But calling the get in java (same way as I do for the post) does not work. It like in the android app the connection can not be make with my server.
I put these permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can someone help me please??
Thank you.
PS:
Still the same error with replaceing localhost with 10.0.2.2. I am copying the complete 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
If you are debugging in a local server, change the IP you want to access, and set this one: 10.0.2.2. So, your httpost will look this way:
HttpPost httppost = new HttpPost("http://10.0.2.2:8888/mywebapp");
For more info, http://developer.android.com/tools/devices/emulator.html#networkaddresses
这篇关于Android应用程序的doPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!