当我按下某个按钮时,我试图通过 Android 发送一个简单的 HTTP 请求。但是每次我这样做时,我的应用程序都会关闭。为什么?顺便说一句,我是 Android 开发的新手。
我的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.office.school-blog.de/api/check-login.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("User", "Steffen"));
nameValuePairs.add(new BasicNameValuePair("Password", "test1234"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
还有我的日志文件:
03-16 14:36:47.554: D/AndroidRuntime(449): Shutting down VM
03-16 14:36:47.554: W/dalvikvm(449): threadid=1: thread exiting with uncaught exception (group=0x40014760)
03-16 14:36:47.574: E/AndroidRuntime(449): FATAL EXCEPTION: main
03-16 14:36:47.574: E/AndroidRuntime(449): android.os.NetworkOnMainThreadException
03-16 14:36:47.574: E/AndroidRuntime(449): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
03-16 14:36:47.574: E/AndroidRuntime(449): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
03-16 14:36:47.574: E/AndroidRuntime(449): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
03-16 14:36:47.574: E/AndroidRuntime(449): at java.net.InetAddress.getAllByName(InetAddress.java:249)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-16 14:36:47.574: E/AndroidRuntime(449): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-16 14:36:47.574: E/AndroidRuntime(449): at com.shr.khg.DashboardActivity$1.onClick(DashboardActivity.java:89)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.view.View.performClick(View.java:3110)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.view.View$PerformClick.run(View.java:11934)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.os.Handler.handleCallback(Handler.java:587)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:132)
03-16 14:36:47.574: E/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:4123)
03-16 14:36:47.574: E/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 14:36:47.574: E/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:491)
03-16 14:36:47.574: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-16 14:36:47.574: E/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-16 14:36:47.574: E/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
03-16 14:36:50.064: I/Process(449): Sending signal. PID: 449 SIG: 9
有人知道出了什么问题吗?