我收到一个 keydispatchingtimedout 错误,似乎找不到原因。有什么帮助吗?如您所见,我创建了一个服务器类,我的 android 应该在启动时连接到它,它确实如此。但是在尝试检索来自我的数据库 mysql 的数据时,它会冻结。
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class TutorialActivity extends Activity {
private Socket socket;
private ObjectInputStream dos;
private DataInputStream dis;
public void connect(){
socket = new Socket();
InetSocketAddress ipPort = new InetSocketAddress("192.168.0.10",4444);
try{
socket.connect(ipPort);
dis = new DataInputStream(socket.getInputStream());
dos = new ObjectInputStream(socket.getInputStream());
}catch(Exception e){
Log.d("OUT_CON",e.toString());
}
while (true) {
try {
String msg = dis.readUTF();
if (msg.equals("Hej")) {
Thread.sleep(50);
receiveArrayList();
}
} catch (Exception e) {
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button sButton = (Button) findViewById(R.id.sbutton);
sButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
connect();
}
});
}
public void receiveArrayList() {
try {
FileInputStream fis = new FileInputStream("randomList");
dos = new ObjectInputStream(fis);
ArrayList a= (ArrayList) (dos.readObject());
for(int i = 0; i < a.size(); i++){
Log.d((String)a.get(i), null);
}
dis.close();
} catch (ClassNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server implements Runnable {
private ServerSocket server;
private Socket socket;
private ObjectOutputStream oos = null;
public Server() {
try {
server = new ServerSocket(4444);
new Thread(this).start();
} catch (Exception e) {
System.out.println(e);
}
}
public void run() {
System.out.println("Server running");
while (true) {
try {
socket = server.accept();
sayHi();
} catch (Exception e) {
}
}
}
private void sayHi(){
DataOutputStream dos;
try {
dos = new DataOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
dos.writeUTF("Hej");
sendNames();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendNames() {
final ArrayList<String> drinkar = new ArrayList<String>();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "drycker";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
try{
Statement st = (Statement) con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM drinkar");
while (res.next()) {
String s = res.getString("Namn");
drinkar.add(s);
}
System.out.println(drinkar);
con.close();
}
catch (SQLException s){
System.out.println("SQL code does not execute.");
}
}
catch (Exception e){
e.printStackTrace();
}
ArrayList<String> a = new ArrayList<String>();
a.add("Hejsan");
a.add("Svejsan");
try {
FileOutputStream fos = new FileOutputStream("randomList");
oos = new ObjectOutputStream(fos);
oos.writeObject(drinkar);
oos.flush();
oos.reset();
oos.close();
fos.close();
} catch (Exception e) {
}
}
public static void main(String[] args) {
new Server();
}
}