0

こんにちは、サーバーに変数を送信するアプリを作成しようとしていますが、サーバーに応答を送信しようとすると、アプリが停止し続けます

ここにコードがあります

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

}

実行 (AbstractHttpClient.java:465) 11-01 03:55:04.622: E/AndroidRuntime(1612): com.example.door.MainActivity.open(MainActivity.java:51) 11-01 03:55:04.622: E/AndroidRuntime(1612): ... 14 more 11-01 03:55:09.212: I/Process(1612): シグナルを送信しています。PID: 1612 SIG: 9

よろしくお願いしますジェフ

4

3 に答える 3

1

Android SDK で許可されていないアプリケーションの UI スレッドからネットワーク接続を開始しようとしています。

Android SDK で提供される AsyncTask クラスをサブクラス化して、ネットワーク呼び出しを別のスレッドに分離できます。

ここにリンク http://developer.android.com/reference/android/os/AsyncTask.htmlがあります

それが役に立てば幸い。

于 2013-11-01T08:12:57.483 に答える
0

リクエストのメソッドをバックグラウンド スレッドに移動する必要があります。AsyncTask を使用することをお勧めします。次のように実行できます。

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

    }

}

アイデアは、Android ではメイン スレッドでネットワーク操作を実行できないということです。AsyncTask はあなたの場合の最良の解決策です... doInBackground が開始する前に呼び出される onPreExecute() メソッドもあります。ここでプロセスの初期化を行うか、読み込みウィンドウを表示して onPostExecute() で閉じることができます方法。

これがお役に立てば幸いです。

于 2013-11-01T08:24:10.213 に答える