android中,socket输出流工作正常,输入流就闪退。。啥情况

确定有穷自动机 2015-12-16 10:39:58
服务器可以获得客户端的信息,但是服务器给客户端返回信息的时候,也就是安卓客户端执行输入流的时候,就闪退了。求大神看看这是咋回事啊。。

安卓客户端代码:
package com.example.app;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import java.io.IOException;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

private Button login_button;
private EditText username;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login_button = (Button) findViewById(R.id.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
//开启socket线程
final loginthread mythread=new loginthread();
mythread.start();

login_button.setOnClickListener(new OnClickListener() {
@Override

public void onClick(View v) {
// TODO Auto-generated method stub
//标志位,用来记录输入的用户名和密码是否正确
boolean mark=false;
String m=null;
while(mythread.t==false){

}



loginthread.out.println(username.getText().toString());
loginthread.out.flush();
loginthread.out.println(password.getText().toString());
loginthread.out.flush();
username.setText("");
password.setText("");


try {
m=loginthread.in.readLine();
Log.d("exception", m);
if(m.equals("login")){
mark=true;

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("exception", "IOException_in");
}

if(mark==false){
Toast.makeText(MainActivity.this, "用户名或密码错误",
Toast.LENGTH_SHORT).show();
}
// else{
// Toast.makeText(MainActivity.this, "登陆成功",
// Toast.LENGTH_SHORT).show();
// }



}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



loginthread类如下:

package com.example.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.util.Log;
import android.widget.Toast;

public class loginthread extends Thread {


public Socket socket=null;
public volatile static PrintWriter out=null;
public volatile static BufferedReader in = null;
public String username=null;
public String password=null;
public static boolean t=false;

// public loginthread() {
// username=u;
// password=p;
// }

public static String read() throws IOException {

return in.readLine();

}

public void run(){
try {
socket = new Socket("10.0.2.2", 10000);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("exception", "UnknownHostException");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("exception", "IOException socket");
}
//Log.d("exception", username);
//Log.d("exception", password);
t=true;
}
}

服务器端主要代码:
public void login() throws IOException {
BufferedReader in_1=null;
ServerSocket serverSocket_1=null;
Socket socket_1=null;
PrintWriter out_1=null;
try{
//开始连接数据库
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
java.sql.Connection conn=DriverManager.getConnection(URL,USER,PASSWORD);
//通过数据库的连接操纵数据库,完成增删改查
Statement stmt=(Statement) conn.createStatement();
ResultSet rs=null;


//服务器端开始建立通信
serverSocket_1 = new ServerSocket(PORT);
socket_1=serverSocket_1.accept();
//输入流
out_1 = new PrintWriter(socket_1.getOutputStream());
//输入流
in_1 = new BufferedReader(new InputStreamReader(socket_1.getInputStream()));
//BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//从控制台获取输入的内容

//out_1.println("login");
//out_1.flush();

while(log){
String username=in_1.readLine();
String password=in_1.readLine();

System.out.println("username:"+username);
System.out.println("password:"+password);
rs=stmt.executeQuery("select username,password from app_user");
while(rs.next()){
if((username.equals(rs.getString("username")))&&(password.equals(rs.getString("password")))){
log=false;
break;
}
}
if(log==true){
out_1.println("nologin");
out_1.flush();
System.out.println("用户名或密码错误");
}
else{
out_1.println("login");
out_1.flush();
System.out.println("登录成功");
break;
}
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//in_1.close();
//out_1.close();
socket_1.close();
serverSocket_1.close();

}


}
...全文
324 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文详细介绍了一个基于C++语言开发的疫苗接种和儿童体检系统的设计与实现全过程,涵盖项目背景、目标意义、架构设计、核心功能模块、数据库实现、API接口规范、前后端代码实现及部署应用等多个方面。系统采用分层架构与模块化设计,实现了儿童信息管理、疫苗接种记录、体检数据录入、多用户权限控制、数据统计分析、异常预警、安全加密与日志审计等核心功能,并通过MySQL数据库进行数据持久化,结合Qt实现图形化界面,支持高并发、数据脱敏、多平台对接与自动化部署。项目强调数据安全、隐私保护与系统可扩展性,适用于社区医疗、疾控心、学校及医院等场景。; 适合人群:具备C++编程基础,熟悉数据库操作和基本软件工程程的开发人员、计算机及相关专业学生、医疗信息化项目开发者,以及希望了解完整医疗管理系统开发程的技术人员。; 使用场景及目标:①学习如何使用C++构建完整的医疗信息管理系统;②掌握数据库设计、前后端交互、权限控制、多线程处理和GUI开发等关键技术;③应用于社区卫生服务、学校健康管理、疾控数据统计等实际业务场景,提升儿童健康管理水平;④作为课程设计、毕业设计或实际项目的参考模板。; 阅读建议:建议读者结合文档的代码示例与数据库脚本,搭建本地开发环境进行实践操作,重点关注系统架构设计、模块解耦、安全机制与前后端交互逻辑,深入理解各功能模块的实现原理,并尝试在此基础上进行功能扩展,如接入移动端或增加AI分析模块。

80,471

社区成员

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

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