使用AsyncTask

时间:2016-08-28 13:40:52

标签: android android-asynctask error-log

任何人都可以详细说明这些错误日志条目:

W/System.err(8537):     at com.any.sales.Alerts.toast(Alerts.java:14)
W/System.err(8537):     at com.any.sales.SalesHandler.startStalesSync(SalesHandler.java:31)
W/System.err(8537):     at com.any.sales.Start.act(Start.java:55)
W/System.err(8537):     at com.any.sales.Start.onCreate(Start.java:23)
W/System.err(8537):     at com.any.sales.Alerts.toast(Alerts.java:14)
W/System.err(8537):     at com.any.sales.HttpCall.onPostExecute(HttpCall.java:103)
W/System.err(8537):     at com.any.sales.HttpCall.onPostExecute(HttpCall.java:22)

我的应用程序一直在崩溃,除此之外,ADB日志没有显示任何其他错误日志。我无法理解这些条目试图说的内容。

我正在制作一个应用程序,其中Http类如HttpPost,HttpClient,HttpResponse连接到在线服务器并获取数据。我为此目的创建了一个类HttpCall并扩展了AsyncTask。我一段时间以来一直在使用这个类,它与我的所有项目完美配合,但现在不知何故它运行不正常。

我正在为异常执行printStackTrace,所有我都看到了错误日志的条目。如果我能理解这些条目试图告诉的内容,我可以纠正这个问题。正如您所看到的,它们只是函数名称,类名行号的引用,没有其他消息可以说明错误实际上是什么。这是我的HttpCall类:

public class HttpCall extends AsyncTask<Void, Void, String> {
String url;
List<NameValuePair> params;
AsyncResponse delegate = null;

public HttpCall(String url, List<NameValuePair> params, AsyncResponse responseDelegate) {
    this.url = url;
    this.params = params;
    this.delegate = responseDelegate;
}

private String post(String url, List<NameValuePair> params) {
    // Data Objects
    UrlEncodedFormEntity entity;
    HttpPost post;
    HttpClient client;
    HttpResponse response;
    BufferedReader reader;
    StringBuilder builder;
    String line;

    try {
        // Set Parameters
        entity = new UrlEncodedFormEntity(params);

        // Set Request
        post = new HttpPost(url);
        post.setEntity(entity);

        // Execute Request through Http Client and Store the response
        client = new DefaultHttpClient();
        response = client.execute(post);

        // Read Response to the buffer
        reader = new BufferedReader(
                new InputStreamReader(
                        response.getEntity().getContent()
                )
        );
        builder = new StringBuilder("");
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        reader.close();
        return builder.toString();
    } catch (ConnectTimeoutException ex) {
        ex.printStackTrace();
        return "Unable to process request"
                + "\nDue to a very slow or no active Internet"
                + "\nPlease try again in few seconds..";
    } catch (NoHttpResponseException ex) {
        ex.printStackTrace();
        return "No Response from Server. Please try again in few seconds..";
    } catch (IOException ex) {
        ex.printStackTrace();
        return "Error in IO";
    } catch (Exception ex) {
        ex.printStackTrace();
        return "Unknown Connection Error";
    }
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(Void... voids) {
    try {
        return post(this.url, this.params);
    } catch (Exception ex) {
        ex.printStackTrace();
        return ex.getMessage();
    }
}

@Override
protected void onPostExecute(String result) {
    try {
        super.onPostExecute(result);
        if (delegate != null)
            delegate.onFinish(result);
    } catch (Exception ex) {
        ex.printStackTrace();
        Alerts.toast(ex.getMessage());
    }
}
}

这是我在AsyncTask的onPostExecute中执行的AsyncResponse类

public interface AsyncResponse {
void onFinish(String output);
}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。有趣的是它与Http或Async之类的东西无关。这次我不是通过使用Toast.makeText而是使用新的Toast()来调用toast,而是抛出异常。

奇怪的是printStackTrace没有显示出这一点。