我在这里不知所措。我正在尝试使用从 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 版本时遇到问题)。仍然不知道是什么让我对另一个图书馆感到悲伤。