Android 正在尝试向服务器发送帖子,但应用程序一直处于死机状态

Android 正在尝试向服务器发送帖子,但应用程序一直处于死机状态,android,http,post,Android,Http,Post,您好,我正在尝试创建一个向服务器发送变量的应用程序,但当我尝试向服务器发送响应时,它会一直停止 这是密码 包com.example.door; 导入java.io.IOException; 导入java.io.UnsupportedEncodingException; 导入java.util.ArrayList; 导入java.util.List; 导入org.apache.http.NameValuePair; 导入org.apache.http.client.ClientProtocolE

您好,我正在尝试创建一个向服务器发送变量的应用程序,但当我尝试向服务器发送响应时,它会一直停止

这是密码

包com.example.door; 导入java.io.IOException; 导入java.io.UnsupportedEncodingException; 导入java.util.ArrayList; 导入java.util.List; 导入org.apache.http.NameValuePair; 导入org.apache.http.client.ClientProtocolException; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.entity.UrlEncodedFormEntity; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.message.BasicNameValuePair; 导入android.os.Bundle; 导入android.app.Activity; 导入android.util.Log; 导入android.view.Menu; 导入android.view.view; 导入android.widget.Button; 公共类MainActivity扩展了活动{ 按钮开门; 按钮闭门; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //为按钮创建对象 //sendButton=(按钮)findViewById(R.id.sendButton); 按钮openbutton=(按钮)findViewById(R.id.openbutton); 按钮关闭按钮=(按钮)findViewById(R.id.closebutton); } //单击“发送”按钮时 公共空间开放(视图五) { 字符串服务器=”http://devel.foo.bar/04r0450240"; HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(服务器); 试一试{ List nameValuePairs=新的ArrayList(1); 添加(新的BasicNameValuePair(“led”,“1”)); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); 试一试{ httpclient.execute(httppost); }捕获(不支持的编码异常e){ e、 printStackTrace(); } }捕获(IOE异常){ //TODO自动生成的捕捉块 i(“HTTP失败”,例如toString()); } 返回; } 公共空间关闭(视图五){ HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(“http://devel.foo.bar/04r0450240"); 试一试{ List nameValuePairs=新的ArrayList(1); 添加(新的BasicNameValuePair(“led”,“0”)); setEntity(新的UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); }捕获(客户端协议例外e){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); } } @凌驾 公共布尔onCreateOptions菜单(菜单){ //为菜单充气;这会将项目添加到操作栏(如果存在)。 getMenuInflater().充气(R.menu.main,menu); 返回true; } }
错误 11-01 03:55:04.622:E/AndroidRuntime(1612):致命异常:主 11-01 03:55:04.622:E/AndroidRuntime(1612):java.lang.IllegalStateException:无法执行活动的方法 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.view.view$1.onClick(view.java:3633) 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.view.view.performClick(view.java:4240) 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.view.view$PerformClick.run(view.java:17721) 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.os.Handler.handleCallback(Handler.java:730)上 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.os.Handler.dispatchMessage(Handler.java:92)上 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.os.Looper.loop(Looper.java:137) 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.app.ActivityThread.main(ActivityThread.java:5103)上 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.lang.reflect.Method.Invokenactive(本机方法) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.lang.reflect.Method.invoke(Method.java:525) 11-01 03:55:04.622:E/AndroidRuntime(1612):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-01 03:55:04.622:E/AndroidRuntime(1612):在dalvik.system.NativeStart.main(本机方法) 11-01 03:55:04.622:E/AndroidRuntime(1612):由以下原因引起:java.lang.reflect.InvocationTargetException 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.lang.reflect.Method.Invokenactive(本机方法) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.lang.reflect.Method.invoke(Method.java:525) 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.view.view$1.onClick(view.java:3628) 11-01 03:55:04.622:E/AndroidRuntime(1612):。。。还有11个 11-01 03:55:04.622:E/AndroidRuntime(1612):由以下原因引起:android.os.NetworkOnMainThreadException 11-01 03:55:04.622:E/AndroidRuntime(1612):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于java.net.InetAddress.getAllByName(InetAddress.java:214) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 11-01 03:55:04.622:E/AndroidRuntime(1612):位于org.apache.http.impl.client.DefaultRequestDirecto package com.example.door; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button opendoor; Button closedoor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Make Object for Buttons // sendButton = (Button) findViewById(R.id.sendButton); Button openbutton = (Button) findViewById(R.id.openButton); Button closebutton = (Button) findViewById(R.id.closeButton); } //When the send button is clicked public void open(View v) { String server ="http://devel.foo.bar/04r0450240"; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost =new HttpPost(server); try{ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("led", "1")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); try { httpclient.execute(httppost); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block Log.i("HTTP Failed", e.toString()); } return; } public void close(View v){ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://devel.foo.bar/04r0450240"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("led", "0")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Button opendoor;
    Button closedoor;

    private static final int CLOSE_METHOD = 1;
    private static final int OPEN_METHOD = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Make Object for Buttons
        // sendButton = (Button) findViewById(R.id.sendButton);
        Button openbutton = (Button) findViewById(R.id.openButton);
        Button closebutton = (Button) findViewById(R.id.closeButton);

    }

    // When the send button is clicked
    public void open(View v) {
        BackgroundTask openTask = new BackgroundTask();
        openTask.execute(OPEN_METHOD);
        return;
    }

    public void close(View v) {
        BackgroundTask closeTask = new BackgroundTask();
        closeTask.execute(CLOSE_METHOD);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class BackgroundTask extends AsyncTask<Integer, Void, String> {

        @Override
        protected String doInBackground(Integer... params) {
            switch (params[0]) {
            case CLOSE_METHOD:
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://devel.foo.bar/04r0450240");
                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            1);
                    nameValuePairs.add(new BasicNameValuePair("led", "0"));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    httpclient.execute(httppost);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case OPEN_METHOD:
                String server = "http://devel.foo.bar/04r0450240";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(server);
                try {
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            1);
                    nameValuePairs.add(new BasicNameValuePair("led", "1"));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    try {
                        httpclient.execute(httppost);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    Log.i("HTTP Failed", e.toString());
                }
                break;
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // Here you can do something after the Asynctask has finished.
            super.onPostExecute(result);
        }

    }

}