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();

}


}
...全文
302 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,350

社区成员

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

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