AsyncTask创建导致崩溃

Red*_*irt 8 android android-asynctask

在扩展AsyncTask的自定义类中遇到一些问题.我的应用是针对Android 4.0.3,以下代码适用于30多人测试它.但是,当我调用新的AsyncRequest时,有两个用户看到应用程序崩溃,如下所示.

我有一个工作记录器正在记录到用户存储上的文本文件,并且不记录AsyncRequest构造函数中的条目.所以我必须假设在调用构造函数之前发生了崩溃.

遭遇此次崩溃的两个设备之一显然是运行Android 4.0.4.不确定其他设备正在运行.不幸的是,我无法访问这两个设备,因此无法看到logcat输出.

任何有关对象创建导致崩溃的输入都将非常感激.

String url = "www.google.com";

new AsyncRequest(callback, context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
Run Code Online (Sandbox Code Playgroud)

这是完整的AsyncRequest类

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);
}
}
Run Code Online (Sandbox Code Playgroud)

编辑:根据下面的评论.

这是我称之为自定义AsyncTask的完整方法.我创建AsyncTask的所有日志消息都显示在日志中.没有例外.

日志记录在创建AsyncRequest之前显示url值,并且URL根本没有格式错误.这就是我所期待的.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

dbe*_*m22 0

首先,请记住,这executeOnExecutor()在 Api 11 之前不可用。您已经说过问题出在 4.0.4 设备上,但请记住这一点。

以下是我为解决问题所要采取的步骤。似乎您已经用所有这些ReportInfo()语句完成了其中一些操作。

首先,我假设您的调用GetServerInfoAsync是在 a 内try...catch,对吗?我正在检查是因为您使用了Throw. 另外,您已经添加了日志记录来检查 url 是否有错误。由于错误发生在您实际使用之前,因此错误不可能与 url 或任何互联网权限有关。callback您可以通过引用和来调用 AsyncTask 生成context。您已经通过ReportInfo()引用上下文添加了日志记录,并且这些工作正常,是吗?因此,上下文不是你的问题。然而,你永远不会检查callback是什么。如果它为空,你会抛出一个错误,但在调用之前你不会对它做任何事情AsyncRequest。尝试一下ReportInfo(callback.toString())看看它是什么。

如果所有其他方法都失败,则似乎是线程错误。为什么不尝试仅使用 AsyncTask,而不是executeOnExecutor(). 你真的需要超过 1 个后台线程吗?