gpt4 book ai didi

android - LAN socket programming - 获取空指针和地址使用异常

转载 作者:行者123 更新时间:2023-11-30 03:26:20 27 4
gpt4 key购买 nike

我已经完成了一个应用程序,其中 android 应用程序将数据发送到 java 桌面 swing 应用程序,以及使用 TCP 套接字编程通过 wifi 将数据从桌面发送到 android。

该应用程序是一个酒店厨房订单预订系统

问题描述 Dine_Tables 类包含代表酒店中每张 table 的按钮,例如单击 table1 按钮时它会启动 BackgroundServers Asyntask 运行服务器以接收桌面应用程序数据也需要从 Dinein_Tables.javaFood_Customizer.java 的 Activity 。

Food_Customizer.java 中,单击提交按钮会启动 ServersendAsyncAction Asyntask,它将一些数据发送到桌面 swing 应用程序。

处理后的桌面应用程序将一些数据发送到 android 应用程序,在 android 应用程序中运行的服务器在收到数据后再次从 Food_Customizer.javaDinein_Tables.java BackgroundServers Asyntask onPostExecute 方法中的 Activity 。

问题是,当我执行此过程时,由于地址使用和 socket = serverSocket.accept(); 处的 Null-Pointer 异常,应用程序停止了两到三次; BackgroundServers 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 ***************************/
}

最佳答案

很明显,您将服务器设置在端口 9999 上:

serverSocket = new ServerSocket(9999);

但是你在端口 4444 上连接到服务器:

socket = new Socket("192.168.1.74", 4444);

确保连接到正确的端口号,否则它将无法工作。希望这会有所帮助。

关于android - LAN socket programming - 获取空指针和地址使用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18169027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com