我正在开发一个用于网络练习的 Android 应用程序,但我遇到了各种各样的问题。我有一个在我的计算机上运行的工作 C 服务器,我正在编写的应用程序旨在连接到服务器,进行身份验证,然后启动从服务器到客户端的文件传输。我对 Android 很陌生,最近才知道阻塞 UI 线程通常是被禁止的。
我创建了一个 AsyncTask 来帮助我解决这个问题。但是,当我调用它时,它完全失败了。但是,它并没有明显失败。我看到的唯一问题是我的主屏幕缩小并奇怪地四处移动。有时 UI 线程完全阻塞,操作系统告诉我我的应用程序没有响应,这对我来说没有任何意义,因为我正在处理阻塞的操作!
这是我用于 AsyncTask 的代码:
private class LoginTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... input) {
int count = input.length;
if (count != 4)
return false;
String ipAddress = input[0];
int portNumberInt = Integer.parseInt(input[1]);
String username = input[2];
String password = input[3];
// Step 0: Establish a connection to the server
PrintWriter out;
BufferedReader in;
try {
serverSocket = new Socket(ipAddress, portNumberInt);
out = new PrintWriter(serverSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
} catch (IOException e) {
((TextView)findViewById(R.id.textView1)).setText(e.getMessage());
((TextView)findViewById(R.id.textView2)).setText("");
return false;
}
// Step 1: send "Authorize" to the server
out.print("Authorize");
// Step 2: server sends a random 64 character challenge string
char[] buffer = new char[64];
String challenge = null;
try {
in.read(buffer);
challenge = new String(buffer);
} catch (IOException e) {
((TextView)findViewById(R.id.textView1)).setText(e.getMessage());
((TextView)findViewById(R.id.textView2)).setText("");
return false;
}
challenge = username + password + challenge;
// Step 3: Compute MD5 hash of username + password + challenge and send it to the server
MessageDigest md5;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
return false;
}
byte[] digest = md5.digest(challenge.getBytes());
out.print(digest);
// Step 4: Server computes the same hash, determines whether or not to accept the connection
String authResult;
try {
in.read(buffer);
authResult = new String(buffer);
} catch (IOException e) {
((TextView)findViewById(R.id.textView1)).setText(e.getMessage());
((TextView)findViewById(R.id.textView2)).setText("");
return false;
}
if (authResult.equals("Pass")) {
return true;
} else {
return false;
}
}
}
这是调用 AsyncTask 的代码:
public boolean authenticate(final String ipAddress, final int portNumberInt, final String username, final String password) {
try {
Toast.makeText(getApplicationContext(), "Starting async task...", Toast.LENGTH_LONG).show();
boolean result = new LoginTask().execute(ipAddress, Integer.toString(portNumberInt), username, password).get();
Toast.makeText(getApplicationContext(), "Async task done!", Toast.LENGTH_LONG).show();
return result;
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
return false;
} catch (ExecutionException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
return false;
}
}
什么可能导致这个问题?我正在运行我的服务器,但它没有显示任何已建立连接的迹象。如果是这种情况,那么我的代码永远不应该阻塞......对吗?