Android 套接字数据流传输到其他活动
我正在做一个套接字编程,其中客户端有两个活动。来自服务器的不同类型的数据用于在两个活动上显示。我在第一个活动中创建了一个套接字,打开Inputdatastream,然后使用标准套接字处理程序类将套接字传输到第二个活动。但是,当我在第二个窗口中打开InputDatastream时,那里没有任何数据。请给我建议一些解决办法。 主要活动:Android 套接字数据流传输到其他活动,android,sockets,android-studio-3.0,Android,Sockets,Android Studio 3.0,我正在做一个套接字编程,其中客户端有两个活动。来自服务器的不同类型的数据用于在两个活动上显示。我在第一个活动中创建了一个套接字,打开Inputdatastream,然后使用标准套接字处理程序类将套接字传输到第二个活动。但是,当我在第二个窗口中打开InputDatastream时,那里没有任何数据。请给我建议一些解决办法。 主要活动: public class MainActivity extends AppCompatActivity { Map<String, Map<Stri
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";
}
}
}
}
你能提供一些你尝试过的代码和得到的结果吗?@RakibulHaq请检查编辑过的代码部分。我只发布了相关部分。代码可以正确编译和运行,但在“活动”屏幕中看不到任何输出。非常感谢。按下连接按钮时,是否可以提供
logcat
日志?出于好奇,您为什么要在同一个应用程序中使用socket
,将数据从一个活动
发送到另一个活动?@RakibulHaq我正在从服务器获取数据,我没有在这里发布代码。只有一部分数据将显示在第一个活动中,其余数据将显示在第二个活动中。我只是尝试传输套接字对象并打开一个新的数据流进行读取,但失败了。我找到了解决这个问题的方法,这个问题已经得到了回答。非常感谢Rakibul。我在这里找到了解决方案:。这个问题是重复的。请转到此链接。
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