我正在開發一個用於網絡練習的Android應用程序,我遇到各種各樣的問題。我有一臺運行在服務器上的C服務器,我正在編寫的應用程序旨在連接到服務器,進行身份驗證,然後啓動從服務器到客戶端的文件傳輸。我對Android非常陌生,而且我最近才瞭解到阻止UI線程通常是被禁止的。Android與AsyncTask的網絡問題
我已經創建了一個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;
}
}
怎麼可能是造成這個問題呢?我正在運行我的服務器,並沒有顯示任何連接已建立的跡象。如果是這種情況,那麼我的代碼不應該阻止...對嗎?
我沒有看到任何阻止代碼,但有一點要注意的是,「你不能在UI線程中阻塞工作」的對口是「你可以從後臺線程訪問UI「。請閱讀[本文檔](http://developer.android.com/resources/articles/painless-threading.html)以獲得對所有這些內容的良好解釋。 – kabuko 2012-04-05 00:02:01