如何传递对象流(exmple:的BufferedReader或DataOutputStream类Ñ等),以其他活动(从Client_layoutActivity.class到chat_wall.class)如果我使用意图方法这样的:通对象流使用意图方法
Intent i = new Intent(Client_layoutActivity.this, chat_wall.class);
startActivity(i);
我做了一个由几个页面组成的聊天应用程序。 第一页是登录页面。在第一页中,客户端使用套接字与服务器进行通信。 登录过程成功后,服务器将发送“登录成功”。 之后,客户端将从第一页(登录页面)更改为第二页面(聊天墙页面)。 我的意思是从第一页使用套接字,BufferedReader和DataOuputstream方法(我假设套接字登录过程仍然连接,所以我仍然可以使用此套接字在第二页进行通信 - 聊天过程)。所以我想传递对象Socket,BufferedReader和DataOutputstream到第二页来使用这个。我写我的代码波纹管:
PAGE 1:登录目的
public void login(){
try {
String name = usrname.getText().toString(); // usrname is android object edit
text
String pass = password.getText().toString();// password is android
object edit text
String sending = "login."+name + "." + pass + "." +"\n";
System.out.println("Sending Message: "+sending);
Socket clientsock = new Socket("192.168.136.6", 28000);
System.out.println("connect to the server...");
BufferedReader in = new BufferedReader(new
InputStreamReader(clientsock.getInputStream()));
DataOutputStream out = new DataOutputStream(clientsock.getOutputStream());
out.writeBytes(sending);
String line = in.readLine();
if (line.contentEquals("login success")){
Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "receive data from
server:"+clientsock.getInetAddress(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(Client_layoutActivity.this, chat_wall.class);
startActivity(i);
} else {
Toast.makeText(this, "Error ", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
System.out.println(e);
System.out.println("Connection Failed");
}
第2页:用于聊天目的
package com.willis.layout;
import java.io.*;
import java.net.*;
import android.widget.*;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.R.integer;
public class chat_wall extends Activity {
Client_layoutActivity ob;
public EditText chatroom;
public Button sendbtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chatwall);
sendbtn = (Button)findViewById(R.id.sendmsg);
sendbtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
sendingmsg();
}
});
chatroom = (EditText)findViewById(R.id.chatroom);
}
public void sendingmsg(){
try
{
BufferedReader in = new BufferedReader(new
InputStreamReader(clientsock.getInputStream()));
DataOutputStream out = new DataOutputStream(clientsock.getOutputStream());
String name = chatroom.getText().toString();
String send = "chat."+name+". \n";
System.out.println("Sending message: "+send);
out.writeBytes(send);
String msgin = in.readLine();
Toast.makeText(this, msgin, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
System.out.println(e);
System.out.println("Connection Failed");
}
}
}
尝试http://stackoverflow.com/questions/2139134/how-to-send-an-object-from- one-android-activity-to-another-using-intents – Som
你不能使用Intents。 – L7ColWinters
可以请你给我们提供什么,你正在试图做的对象流(为什么它从一个活动到下一个?) – L7ColWinters