在我决定联系这里之前,我已经在 Stackoverflow 和互联网上查看了大部分 q/a 以获得答案。
我正在尝试在我的网站上调用 php 页面,并且在调用 httpclient.execute 时不断收到此错误:
08-19 12:42:33.889: E/AndroidRuntime(5340): FATAL EXCEPTION: main 08-19 12:42:33.889:
E/AndroidRuntime(5340): java.lang.IllegalStateException: Could not execute method of the activity 08-19 12:42:33.889: E/AndroidRuntime(5340):
at android.view.View$1.onClick(View.java:3046)
所以我通读了一些帖子,一件事我确定我有
<uses-permission android:name="android.permission.INTERNET" />
添加到我的清单(我这样做)下面是我从互联网上获得的一个片段,我从一个按钮 onclick 事件中调用它:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite/scripts/test.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("function", "1"));
nameValuePairs.add(new BasicNameValuePair("user", "username"));
nameValuePairs.add(new BasicNameValuePair("pass", "123"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.e("log_tag", response.toString());
} catch (ClientProtocolException e) {
Log.e("log_tag", e.toString());
} catch (IOException e) {
Log.e("log_tag", e.toString());
}
完整日志:
08-19 12:42:16.365: I/System.out(5340): Sending WAIT chunk
08-19 12:42:16.365: W/ActivityThread(5340): Application com.example.equinetravels is waiting for the debugger on port 8100...
08-19 12:42:16.373: I/dalvikvm(5340): Debugger is active
08-19 12:42:16.373: I/System.out(5340): Debugger has connected
08-19 12:42:16.373: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.576: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.779: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:16.982: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.186: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.381: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.584: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.779: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:17.982: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.186: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.381: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.584: I/System.out(5340): waiting for debugger to settle...
08-19 12:42:18.787: I/System.out(5340): debugger has settled (1432)
08-19 12:42:19.037: D/libEGL(5340): loaded /system/lib/egl/libGLES_android.so
08-19 12:42:19.045: D/libEGL(5340): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
08-19 12:42:19.053: D/libEGL(5340): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
08-19 12:42:19.053: D/libEGL(5340): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
08-19 12:42:19.162: D/OpenGLRenderer(5340): Enabling debug mode 0
08-19 12:42:33.834: D/AndroidRuntime(5340): Shutting down VM
08-19 12:42:33.834: W/dalvikvm(5340): threadid=1: thread exiting with uncaught exception (group=0x40ab6210)
08-19 12:42:33.889: E/AndroidRuntime(5340): FATAL EXCEPTION: main
08-19 12:42:33.889: E/AndroidRuntime(5340): java.lang.IllegalStateException: Could not execute method of the activity
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.view.View$1.onClick(View.java:3046)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.view.View.performClick(View.java:3526)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.view.View$PerformClick.run(View.java:14133)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.os.Handler.handleCallback(Handler.java:605)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.os.Looper.loop(Looper.java:137)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.app.ActivityThread.main(ActivityThread.java:4697)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:42:33.889: E/AndroidRuntime(5340): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-19 12:42:33.889: E/AndroidRuntime(5340): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-19 12:42:33.889: E/AndroidRuntime(5340): at dalvik.system.NativeStart.main(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340): Caused by: java.lang.reflect.InvocationTargetException
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.view.View$1.onClick(View.java:3041)
08-19 12:42:33.889: E/AndroidRuntime(5340): ... 11 more
08-19 12:42:33.889: E/AndroidRuntime(5340): Caused by: android.os.NetworkOnMainThreadException
08-19 12:42:33.889: E/AndroidRuntime(5340): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1119)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.net.InetAddress.lookupHostByName(InetAddress.java:441)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
08-19 12:42:33.889: E/AndroidRuntime(5340): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-19 12:42:33.889: E/AndroidRuntime(5340): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-19 12:42:33.889: E/AndroidRuntime(5340): at com.example.equinetravels.MainActivity.postData(MainActivity.java:79)
08-19 12:42:33.889: E/AndroidRuntime(5340): at com.example.equinetravels.MainActivity.sendLogon(MainActivity.java:47)
08-19 12:42:33.889: E/AndroidRuntime(5340): ... 14 more