100分悬赏android多socket第二个activity获取socket闪退

malan6810 2017-09-19 03:52:01
这是主页面
public class MainActivity extends AppCompatActivity {
private Button login;
private EditText pwd;
Socket socket;
private ObjectInputStream in = null;
private ObjectOutputStream os = null;
private Object success;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.login);
pwd = (EditText)findViewById(R.id.pwd);

new Thread(){
@Override
public void run() {
try {
startConnent();
}catch (Exception e){}
}
}.start();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
String password = pwd.getText().toString();
if (socket.isConnected()) {
if (!socket.isOutputShutdown()) {
os = ((MySocket) getApplication()).getOs();
ResultData abc = new ResultData();
abc.setMsg(password);
abc.setNum(XieyiNum.login);
abc.setTime(new Date());
os.writeObject(abc);
os.flush();
}
}
}catch (Exception e){}

}
});
}

private void startConnent()throws Exception{
socket = ((MySocket) getApplication()).getSocket();
os = ((MySocket) getApplication()).getOs();
in = ((MySocket) getApplication()).getIn();
while (true) {
if (socket.isConnected()) {
if (!socket.isInputShutdown()) {
if ((success = in.readObject()) != null) {
if (((ResultData)success).getMsg().equals("success")&&((ResultData)success).getNum()==XieyiNum.login){
startActivity(new Intent(this,ChatActivity.class));
}
}
}
}
}
}
}
第二个页面
public class ChatActivity extends AppCompatActivity{
private Button send,close;
private EditText edit;
private TextView ip,receieve;
private String receieveMsg;
private Socket socket;
private ObjectInputStream in = null;
private ObjectOutputStream os = null;
private Object msg;
private String sendMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
send=(Button)findViewById(R.id.send);
close=(Button)findViewById(R.id.close);
receieve=(TextView)findViewById(R.id.receieve);
ip=(TextView)findViewById(R.id.ip);
edit=(EditText)findViewById(R.id.edit);
new Thread(){
@Override
public void run() {
try {
startConnent();
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
ip.setText(socket.getInetAddress().toString());
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMsg=edit.getText().toString();
if (!sendMsg.equals("")&&sendMsg!=null){
edit.setText("");
try {
toServerMsg(sendMsg);
}catch (Exception e){
e.printStackTrace();
}
}
}
});
}

private void toServerMsg(String msg)throws Exception{
ResultData r=new ResultData();
r.setMsg(msg);
r.setTime(new Date());
r.setNum(XieyiNum.send);
os.writeObject(r);
os.flush();
}

private void addMsg(String msg){
receieveMsg=receieve.getText().toString();
receieveMsg+="\n";
receieveMsg+=msg;
receieve.setText(receieveMsg);
}

private void startConnent()throws Exception {
socket = ((MySocket) getApplication()).getSocket();
os = ((MySocket) getApplication()).getOs();
in = ((MySocket) getApplication()).getIn();
while (true) {
if (socket.isConnected()) {
if (!socket.isInputShutdown()) {
if ((msg = in.readObject()) != null) {
if (((ResultData)msg).getNum()== XieyiNum.send){
addMsg(((ResultData)msg).getMsg());
}
}
}
}
}
}
}
这是application获取socket
public class MySocket extends Application {
private static final String host="192.168.162.2";
private static final int port=12345;
private static ObjectOutputStream os;
private static ObjectInputStream in;
private static Socket socket = null;

@Override
public void onCreate() {
super.onCreate();
getSocket();
getOs();
getIn();
}

public Socket getSocket(){
if(socket==null){
try {
socket= new Socket(host,port);
}catch (Exception e){
e.printStackTrace();
}
}
return socket;
}

public ObjectInputStream getIn() {
if(in==null){
try {
in = new ObjectInputStream(new BufferedInputStream(this.socket.getInputStream()));
}catch (Exception e){
e.printStackTrace();
}
}
return in;
}

public ObjectOutputStream getOs() {
if(os==null){
try {
os = new ObjectOutputStream(this.socket.getOutputStream());
}catch (Exception e){
e.printStackTrace();
}
}
return os;
}

@Override
public void onTerminate() {
super.onTerminate();
try {
socket.close();
}catch (Exception e){}

}
}

哪位大牛方便知道一下,菜鸡没有写过android,原来写服务端的,感激不尽
...全文
290 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
taugnan 2017-09-22
  • 打赏
  • 举报
回复
把日志贴出来更好一点。
taugnan 2017-09-22
  • 打赏
  • 举报
回复
打印一下日志就知道哪里报错了。
chengzhouyang11 2017-09-22
  • 打赏
  • 举报
回复
日志好歹贴一下

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧