我正在进行套接字编程,其中客户端有2个活动。并且来自服务器的不同类型的数据用于在两个活动上显示。我在第一个活动中创建了一个套接字,打开了Inputdatastream,然后使用标准的Socket Handler类将套接字转移到第二个活动。但是当我在第二个打开InputDatastream时,我没有得到任何数据。请给我一些解决方案。 MainActivity:
public class MainActivity extends AppCompatActivity {
Map<String, Map<String,Integer>> order = new HashMap<>();
Map<String, Integer> details = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buttonConnect.setOnClickListener(buttonConnectOnClickListener);
if(!(total>0.0)) {
Toast.makeText(this, "nothing selected", Toast.LENGTH_LONG).show();
}
}
OnClickListener buttonConnectOnClickListener =
new OnClickListener(){
@Override
public void onClick(View arg0) {
MyClientTask myClientTask = new MyClientTask(
ipToset,
Integer.parseInt(portToset));
myClientTask.execute();
}};
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port) {
dstAddress = addr;
dstPort = port;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
SocketHandler.setSocket(socket);
if(total>0.0) {
SocketClientReplyThread socketClientReplyThread = new SocketClientReplyThread(
socket, "from client");
socketClientReplyThread.run();
SocketClientReadThread socketClientReadThread = new SocketClientReadThread(
socket, "from client");
socketClientReadThread.run();
}
if(total>0.0) {
Intent intent = new Intent(MainActivity.this.getApplicationContext(), OrderSummary.class);
startActivity(intent);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class SocketClientReadThread extends Thread {
private Socket hostThreadSocket;
String message;
SocketClientReadThread(Socket socket, String c) {
hostThreadSocket = socket;
message = c;
}
@Override
public void run() {
OutputStream outputStream;
try {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(hostThreadSocket.getInputStream()));
response= inFromServer.readLine();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textResponse.setText(response);
}
});
response= inFromServer.readLine();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textResponse.setText(response);
}
});
} catch (IOException e) {
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
}
}
}
SecondActivity:
public class OrderSummary extends AppCompatActivity {
TextView prep;
TextView pack;
TextView ready;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_summary);
TextView prep = (TextView) findViewById(R.id.preparing);
TextView pack = (TextView) findViewById(R.id.packaging);
TextView ready = (TextView) findViewById(R.id.ready);
ready.setText("predators");
OrderSummary.MyTask myTask = new OrderSummary.MyTask();
myTask.execute();
}
public class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0){
System.out.println("background");
try {
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(SocketHandler.getSocket().getInputStream()));
final String prep1 = inFromServer.readLine();
final String pack1 = inFromServer.readLine();
final String ready1 = inFromServer.readLine();
OrderSummary.this.runOnUiThread(new Runnable() {
@Override
public void run() {
prep.setText(prep1);
pack.setText(pack1);
ready.setText("predators");
}
});
}catch(IOException e){}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
}
点击连接按钮时记录:
04-22 06:58:33.415 28714-28725/? I/zygote: Background concurrent copying GC freed 4134(2MB) AllocSpace objects, 0(0B) LOS objects, 61% free, 958KB/2MB, paused 4.538ms total 246.184ms
04-22 06:59:36.811 28714-28725/com.example.ashwa.testclient I/zygote: Background concurrent copying GC freed 277(277KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 2MB/4MB, paused 79.877ms total 1.690s
04-22 06:59:37.308 28714-29208/com.example.ashwa.testclient I/System.out: background