gpt4 book ai didi

Android应用程序doPost

转载 作者:行者123 更新时间:2023-11-29 22:02:54 25 4
gpt4 key购买 nike

我在执行从我的 Android 应用程序到我的 Google 应用程序的简单 http 发布请求时遇到问题。

所以这是错误信息:

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

它发生在我的 android 应用程序中的 httpclient.execute 命令中:

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);
}

在我的服务器上没有我到达我的 post 方法的痕迹:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException {
System.out.println("SYL: POST request received");
super.doPost(req, resp);
}

服务器是 google webb 应用程序,在我的 eclipse 下本地运行,配置如下:

<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>

如果测试我通过 Web 导航器调用的 get 方法,它会起作用。但是在 java 中调用 get(与我对帖子所做的方式相同)不起作用。就像在 android 应用程序中一样,无法与我的服务器建立连接。

我设置了这些权限:

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

有人可以帮帮我吗??

谢谢。

附言:

用 10.0.2.2 替换 localhost 还是一样的错误。我正在复制完整的痕迹:

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

最佳答案

如果你是在本地服务器调试,更改你要访问的IP,设置这个:10.0.2.2。所以,你的 httppost 看起来是这样的:

HttpPost httppost = new HttpPost("http://10.0.2.2:8888/mywebapp");

欲了解更多信息,http://developer.android.com/tools/devices/emulator.html#networkaddresses

关于Android应用程序doPost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com