2

我在这里不知所措。我正在尝试使用从 RESTful API 中提取的数据在我的 Activity 中填充一个列表。我使用 Apache HttpClient 库 (org.apache.http.client) 创建了与 API 对话(成功)的 API 层。但是,当我尝试将 Activity 与 API 层(通过 AsyncTask)连接时,灾难接踵而至。我从有问题的代码中删除了 API 层,而是嵌入了我能想到的最简单的示例来尝试隔离问题(无济于事)。这是有问题的代码:

public class MyActivity extends Activity {

@Override
protected void onCreate( Bundle savedInstanceState ) {

    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );
}

@Override
protected void onResume() {

    super.onResume();
    new GetEventsTask().execute();
}

private class GetEventsTask extends AsyncTask<Object, Integer, List<IEvent>> {

    @Override
    protected List<IEvent> doInBackground( Object... params ) {

        fetch();
        return null;
    }

    @Override
    protected void onPostExecute( List<IEvent> events ) {

        Toast.makeText( getApplicationContext(), "Success", Toast.LENGTH_SHORT ).show();
    }

    private void fetch() {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost( "https://someurl.com/login" );

        List<NameValuePair> data = new ArrayList<NameValuePair>();
        data.add( new BasicNameValuePair( "username", "hai" ) );
        data.add( new BasicNameValuePair( "password", "kthxbye" ) );

        try {
            httpPost.setEntity( new UrlEncodedFormEntity( data ) );
            CloseableHttpResponse response = client.execute( httpPost );
        } catch( Exception e ) {
            Toast.makeText( getApplicationContext(), "Caught Exception", Toast.LENGTH_SHORT ).show();
        }
    }
}

这是堆栈跟踪:

10-16 19:36:04.264: ERROR/AndroidRuntime(633): FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:200)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    at java.lang.Thread.run(Thread.java:1019)
    Caused by: java.lang.ExceptionInInitializerError
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:727)
    at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
    at ca.redacted.app.MyActivity$GetEventsTask.fetch(MyActivity.java:70)
    at ca.redacted.app.MyActivity$GetEventsTask.doInBackground(MyActivity.java:49)
    at ca.redacted.app.MyActivity$GetEventsTask.doInBackground(MyActivity.java:44)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    ... 4 more
    Caused by: java.lang.ExceptionInInitializerError
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
    at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
    ... 15 more
    Caused by: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
    at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
    ... 18 more

我对 Android 编程比较陌生(并且有一段时间没有接触过 Java),因此非常感谢任何帮助。

更新 基于调试运行,这是抛出 FatalException 的行

CloseableHttpClient 客户端 = HttpClients.createDefault();

再次更新

切换到使用 Android 提供的 DefaultHttpClient 实现解决了这个问题(以防其他人在使用 Apache 版本时遇到问题)。仍然不知道是什么让我对另一个图书馆感到悲伤。

4

2 回答 2

2

我假设您已添加 apache 库,因为 HttpClients 未内置在 Android 代码中,请参见此处。您获得运行时异常但未编译异常的原因我认为是因为您的库未与您的应用程序一起导出。

ExceptionInInitializerError

意味着它无法加载指向此处的静态类(HttpClients):

CloseableHttpClient client = HttpClients.createDefault();

为了解决这个问题,您必须在 eclipse 中将外部 apache 库与应用程序一起导出:

项目-> 配置构建路径-> 选择要导出的库。

除此之外,@codeMagic 指出您的 catch 子句不能使用 Toast,因为它应该在 UI 线程上运行。这不会导致您的异常,因为您当前的代码没有到达 try 子句,而是抬头。

于 2013-10-16T20:12:14.647 回答
1

据我所知,这似乎不是您当前的问题,但在某些时候会成为问题。这些线

catch( Exception e ) {
        Toast.makeText( getApplicationContext(), "Caught Exception", Toast.LENGTH_SHORT ).show();

正在更新,UI并且因为它是从doInBackground()它调用的,所以它在后台运行,Thread这将导致异常。处理此问题的方法是根据您获得的响应类型从那里response传递onPostExecute()和更新。UI

根据Java文档

此异常来自初始化 a 的问题static Class。从这条线追溯

CloseableHttpClient client = HttpClients.createDefault();

看看为什么会失败。

于 2013-10-16T19:59:38.783 回答