-1

当我按下某个按钮时,我试图通过 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

有人知道出了什么问题吗?

4

4 回答 4

2

这将解决您的问题:

class MyHttpPost extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) 
{

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);
    HttpEntity httpEntity = response.getEntity();
                String result = httpEntity.getContent();


        } catch (ClientProtocolException e) {
         // TODO Auto-generated catch block
        } catch (IOException e) {
         // TODO Auto-generated catch block
        }



return true;
                    }

}

替换你的上面的代码public class,因为这个MyHttpPost类是一个内部类,我们不会为它创建一个单独的.java文件。

要使用它:

new MyHttpPost().execute();
于 2013-03-16T14:59:54.723 回答
2

使用异步任务。

不要在主线程上做网络操作:android版本>=3.0是不允许的。

doInBackground在方法中进行网络操作。

在这里阅读。

于 2013-03-16T14:49:56.030 回答
0

Android 不允许在 UI(或主)线程上阻止诸如网络访问之类的任务。这是所有与用户交互有关的线程。因此,您将需要(可能)创建和 AsynTask 类来运行查询并反馈信息。请参阅http://developer.android.com/reference/android/os/AsyncTask.html。此处还给出了 AsyncTask 的示例:http ://www.javasrilankansupport.com/2012/11/asynctask-android-example-asynctask-in.html

于 2013-03-16T14:52:07.800 回答
0

You are trying to perform network operation on UI thread,尝试采取单独的线程异步任务

于 2013-03-16T14:53:03.717 回答