1

AndroidアプリケーションがデータをJavaデスクトップSwingアプリケーションに送信し、Wi-Fiを介してTCPソケットプログラミングを使用してデスクトップからAndroidにデータを送信するアプリケーションを作成しました。

Th アプリケーションは、ホテル キッチンの注文予約システムです。

この問題は、Dine_Tables クラスにホテル内の各テーブルを表すボタンが含まれていることを示しています。たとえば、table1 ボタンをクリックすると、デスクトップ アプリケーション データを受信するためのサーバーを実行するBackgroundServers が開始され、からへのアクティビティが取得されます。AsyntaskDinein_Tables.javaFood_Customizer.java

Food_Customizer.java送信ボタンをクリックすると、デスクトップ Swing アプリケーションにデータを送信する ServersendAsyncAction が開始さ Asyntaskます。

処理後のデスクトップ アプリケーションは、いくつかのデータを Android アプリケーションに送信します。データを受信すると、Android アプリケーションで実行されるサーバーは、 BackgroundServersメソッドのアクティビティにFood_Customizer.java戻ります。Dinein_Tables.java Asyntask onPostExecute

問題は、このプロセスを実行すると、アドレスインの使用とBackgroundServersでのNull-Pointer例外 が原因で、アプリケーションが 2、3 回停止することです。socket = serverSocket.accept(); Asyntask

誰かこの問題の解決策を教えてください

Dinein_Tables.java

public class Dinein_Tables extends Activity {
:
:


 table1.setOnClickListener(new OnClickListener() {

                    public void onClick(final View v) {
                        new Handler().postDelayed(new Runnable() {

                            public void run() {

                                        Food_Customizer.BackgroundServers ob = new Food_Customizer().new BackgroundServers(contexts);
                                        ob.execute("");
                                      Intent toAnotherActivity = new Intent(v.getContext(), Food_Customizer.class);
                                      startActivity(toAnotherActivity);
                                      finish();
                            }

                        }, 100L);    

                    }

                });
}

Food_Customizer.java

public class Food_Customizer extends Activity {
:
:
    submit= (Button)findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          pd = ProgressDialog.show(contexts, "Sending to Server...","Please Wait...", true, false);
          new ServersendAsyncAction().execute();
    }
    });
:
:



      /****************************** AsyncTask ********************************************************/

        private class ServersendAsyncAction extends AsyncTask<String, Void, String> {

            /****************************** AsyncTask doInBackground() ACTION ********************************/
            protected String doInBackground(String... args) {


                Socket socket = null;
                DataOutputStream dataOutputStream = null;
                DataInputStream dataInputStream = null;
                boolean flag = true;
                while (flag) /******** If data is send flag turn to be false *******/
                {
                    try {
                        socket = new Socket("192.168.1.74", 4444);
                        dataOutputStream = new DataOutputStream(socket.getOutputStream());
                        dataInputStream = new DataInputStream(socket.getInputStream());
                        dataOutputStream.writeUTF(datastosend);
                        flag = false;
                        } catch (UnknownHostException e) {
                        flag = true;
                        e.printStackTrace();
                        } catch (IOException e) {
                        flag = true;
                        e.printStackTrace();
                        }

                    /******** CLOSING SOCKET *****************/
                    finally {
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        /******** CLOSING DATAOUTPUTSTREAM *******/
                        if (dataOutputStream != null) {
                            try {
                                dataOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        /******** CLOSING DATAINPUTSTREAM ********/
                        if (dataInputStream != null) {
                            try {
                                dataInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                return null;
                /******** returns what you want to pass to the onPostExecute() *******/
            }

            /****************************** AsyncTask onPostExecute() ACTION *********************************/
            protected void onPostExecute(String result) {

            }

        /********************* ENDING OF ASYN TASK CLASS ServersendAsyncAction ***************************/
        }



        public Context con;
            public static ServerSocket serverSocket = null;

        public class BackgroundServers extends AsyncTask<String, Void, String> {

            public BackgroundServers(Context context) {
                con=context;
            }

            /****************************** AsyncTask doInBackground() ACTION ********************************/
            protected  String doInBackground(String... args) {

                Socket socket = null;
                DataInputStream dataInputStream = null;
                DataOutputStream dataOutputStream = null;

                try {
                    serverSocket = new ServerSocket(9999);
                    System.out.println("Listening :9999");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                while (true) {
                    try {
                        socket = serverSocket.accept();
                        dataInputStream = new DataInputStream(
                        socket.getInputStream());
                        dataOutputStream = new DataOutputStream(socket.getOutputStream());
                        System.out.println("ip: " + socket.getInetAddress());
                        String incoming_message=(dataInputStream.readUTF());
                        incoming_message=incoming_message.replace("/", "");
                        String recdatas[]=incoming_message.split("#");
                        if(recdatas[0].equalsIgnoreCase("success"))
                        {
                          DatabaseConnection dbs=new DatabaseConnection(con);
                          int status=dbs.update("UPDATE hotel_pub_tables SET status='occupied' WHERE tableno='"+recdatas[1]+"'");
                          if(status>0)
                                  {
                              tabelstatus=1;
                              //msg.obj="Table status changed!!!";
                              System.out.println("Table status changed!!!");
                              if (true) {
                                    System.out.println("entered 222");

                                               System.out.println(tabelstatus);
                                               if(tabelstatus==1)
                                                {
                                                   System.out.println(tabelstatus); 
                                                   Food_Customizer.pd.dismiss();
                                                   System.out.println("success"); 

                                                }
                                                else if(tabelstatus==2)
                                                {
                                                    Food_Customizer.pd.dismiss();

                                                  Intent intent = new Intent(Food_Customizer.this, Dinein_Tables.class);
                                                  startActivity(intent);
                                                  finish(); 
                                                }

                                }
                          }
                          else
                              tabelstatus=2;
                          dbs.close();
                        }
                        dataOutputStream.writeUTF("Hello!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (socket != null) {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        if (dataInputStream != null) {
                            try {
                                dataInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        if (dataOutputStream != null) {
                            try {
                                dataOutputStream.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    }
                             return null;
                }

                /******** returns what you want to pass to the onPostExecute() *******/
            }

            /****************************** AsyncTask onPostExecute() ACTION *********************************/
            @Override
            protected void onPostExecute(String result) {
                System.out.println("eneterd on posttttttttttttttt");
                con.startActivity(new Intent(con, Dinein_Tables.class));
                finish();
                }

            }
        }
        /********************* ENDING OF ASYN TASK CLASS BackgroundServers ***************************/
}
4

1 に答える 1