Có một số vấn đề với lớp tùy chỉnh mở rộng AsyncTask. Ứng dụng của tôi đang nhắm mục tiêu Android 4.0.3 và mã bên dưới hoạt động tốt cho hơn 30 người thử nghiệm nó. Tuy nhiên có hai người dùng đang gặp sự cố ứng dụng khi tôi gọi AsyncRequest mới như dưới đây.Tạo AsyncTask gây ra sự cố
Tôi có một trình ghi nhật ký đang hoạt động đang ghi vào tệp văn bản trên bộ nhớ người dùng và không ghi lại mục nhập trong hàm tạo AsyncRequest. Vì vậy, tôi phải giả định rằng sự cố xảy ra trước khi hàm tạo được gọi.
Một trong hai thiết bị gặp sự cố này đang chạy Android 4.0.4. Không chắc chắn thiết bị khác đang chạy. Thật không may tôi không 'có quyền truy cập vào hai thiết bị để không thể nhìn thấy một đầu ra logcat.
Bất kỳ đầu vào nào về lý do tạo đối tượng gây ra sự cố sẽ được đánh giá cao.
String url = "www.google.com";
new AsyncRequest(callback, context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
Và đây là lớp AsyncRequest đầy đủ
public class AsyncRequest extends AsyncTask<String, String, String>{
HttpURLConnection connection;
InputStream inStream;
IApiCallback callback;
Context context_;
public AsyncRequest(IApiCallback callback, Context context) {
// Log entry added for testing. Never gets called.
FileLogger.getFileLogger(context).ReportInfo("Enter AsyncRequest Constructor");
this.callback = callback;
context_ = context;
}
@Override
protected String doInBackground(String... uri) {
try {
URL url = new URL(uri[0] + "?format=json");
FileLogger.getFileLogger(context_).ReportInfo("Async Request: Sending HTTP GET to " + url);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.addRequestProperty("Accept-Encoding", "gzip");
connection.addRequestProperty("Cache-Control", "no-cache");
connection.connect();
String encoding = connection.getContentEncoding();
// Determine if the stream is compressed and uncompress it if needed.
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
inStream = new GZIPInputStream(connection.getInputStream());
} else {
inStream = connection.getInputStream();
}
if (inStream != null) {
// process response
BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
} catch (SocketTimeoutException e) {
FileLogger.getFileLogger(context_).ReportException("Async Request: SocketTimeoutException", e);
Log.i("AsyncRequest", "Socket Timeout occured");
} catch (MalformedURLException e) {
FileLogger.getFileLogger(context_).ReportException("Async Request: MalformedUrlException", e);
} catch (IOException e) {
FileLogger.getFileLogger(context_).ReportException("Async Request: IOException", e);
Log.i("doInBackground:","IOException");
if (e != null && e.getMessage() != null) {
Log.i("doInBackground:",e.getMessage());
}
} catch (Exception e) {
FileLogger.getFileLogger(context_).ReportException("Async Request: Exception", e);
} finally {
if (connection != null)
connection.disconnect();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null)
FileLogger.getFileLogger(context_).ReportInfo("Async Request: Response is valid");
else
FileLogger.getFileLogger(context_).ReportInfo("Async Request: Invalid response");
callback.Execute(result);
}
}
EDIT: Theo ý kiến dưới đây.
Đây là phương pháp đầy đủ mà tôi gọi là AsyncTask tùy chỉnh của mình. Tất cả các thông điệp ghi nhật ký mà tôi có để tạo AsyncTask được hiển thị trong nhật ký. Không có ngoại lệ nào.
Ghi nhật ký hiển thị giá trị url ngay trước khi tạo AsyncRequest của tôi và URL không bị định dạng sai. Đó là những gì tôi đang mong đợi.
public void GetServerInfoAsync(IApiCallback callback, Context context) throws IllegalArgumentException, Exception {
if (callback == null)
throw new IllegalArgumentException("callback");
if (context == null)
throw new IllegalArgumentException("context");
try {
FileLogger.getFileLogger(context).ReportInfo("Build URL");
String url = GetApiUrl("System/Info");
FileLogger.getFileLogger(context).ReportInfo("Finished building URL");
if (url != null) {
FileLogger.getFileLogger(context).ReportInfo("GetServerInfoAsync: url is " + url);
new AsyncRequest(callback, context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
} else {
FileLogger.getFileLogger(context).ReportError("GetServerInfoAsync: url is null");
}
} catch (IllegalArgumentException iae) {
FileLogger.getFileLogger(context).ReportException("GetServerInfoAsync: IllegalArgumentException", iae);
throw iae;
} catch (Exception e) {
FileLogger.getFileLogger(context).ReportException("GetServerInfoAsync: Exception", e);
throw e;
}
}
Nếu bạn đã phát hiện ra lỗi là trước yêu cầu này, hãy hiển thị mã trước khi bạn gọi nó. – Gustek
Bạn đã đăng nhập vào mã trong quá trình chạy để tạo 'AsyncTask' chưa? Có lẽ điều này sẽ cho phép bạn xem dòng nào thực sự thất bại? –
Gustek. Lỗi không xuất hiện trước khi tôi gọi nó. Tôi đã thêm phương thức gọi vào bài đăng gốc của mình. – Redshirt