请求 HttpResponse<JsonNode> 时 Android 应用程序崩溃
Posted
技术标签:
【中文标题】请求 HttpResponse<JsonNode> 时 Android 应用程序崩溃【英文标题】:Android App crashes when requesting HttpResponse<JsonNode> 【发布时间】:2019-11-19 11:05:45 【问题描述】:我目前正在使用 FAROO Web Search 开发一个新闻应用程序,但是当应用程序使用 API 代码查找新闻时,它会崩溃。我不认为这是 API 的错,因为在 RapidApi 的测试中它工作得很好。我已经在 android Manifest 中添加了互联网权限,并且一直在稳定连接使用它。 [![API 测试][1]][1] 如图所示,在网络中测试端点时,它可以毫无问题地获得结果。
我还尝试添加“implementation 'org.apache.httpcomponents:httpclient:4.5'”,甚至排除一些文件,因为它们会产生更多错误。
这是主要活动
static final ArrayList<String> title_array = new ArrayList<String>();
static final ArrayList<String> notice_array = new ArrayList<String>();
static final ArrayList<String> author_array = new ArrayList<String>();
ListView list;
BaseAdapter2 adapter;
ProgressBar loader;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
System.out.println("Enter");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_activity);
list = (ListView) findViewById(R.id.listNews);
loader = (ProgressBar) findViewById(R.id.loader);
if(InternetAvaliable(this))
new TheTask().execute();
class TheTask extends AsyncTask<Void, Void, String>
@Override
protected String doInBackground(Void... params)
String str = null;
try
System.out.println("Here1");
HttpResponse<JsonNode> response = Unirest.get("https://faroo-faroo-web-search.p.rapidapi.com/api?q=food")
.header("X-RapidAPI-Host", "faroo-faroo-web-search.p.rapidapi.com")
.header("X-RapidAPI-Key", "f18432002emshe33f07d9b71559bp125dd7jsn2d9f1840345f")
.asJson();
System.out.println("Here2");
str = response.toString();
catch(UnirestException exception)exception.printStackTrace();
return str;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
String response = result.toString();
try
JSONArray new_array = new JSONArray(response);
for(int i = 0; i < new_array.length(); i++)
try
JSONObject jsonObject = new_array.getJSONObject(i);
if(jsonObject.getBoolean("news"))
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("kwic").toString());
author_array.add(jsonObject.getString("author").toString());
catch (JSONException e)e.printStackTrace();
adapter = new BaseAdapter2(NewsActivity.this, title_array, notice_array, author_array);
list.setAdapter(adapter);
catch (JSONException e)e.printStackTrace();
错误就在这里
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.ecoapp, PID: 13944
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/config/RequestConfig;
at com.mashape.unirest.http.options.Options.refresh(Options.java:70)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
at com.mashape.unirest.http.options.Options.getOption(Options.java:42)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at com.example.ecoapp.NewsActivity$TheTask.doInBackground(NewsActivity.java:79)
at com.example.ecoapp.NewsActivity$TheTask.doInBackground(NewsActivity.java:67)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.config.RequestConfig" on path: DexPathList[[zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/base.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_resources_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.mashape.unirest.http.options.Options.refresh(Options.java:70)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
at com.mashape.unirest.http.options.Options.getOption(Options.java:42)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at com.example.ecoapp.NewsActivity$TheTask.doInBackground(NewsActivity.java:79)
at com.example.ecoapp.NewsActivity$TheTask.doInBackground(NewsActivity.java:67)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.ecoapp-ex8BePcZbze4GqsGRAKURQ==/split_lib_resources_apk.apk
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:353)
at dalvik.system.DexFile.<init>(DexFile.java:100)
at dalvik.system.DexFile.<init>(DexFile.java:74)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:374)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:337)
at dalvik.system.DexPathList.<init>(DexPathList.java:157)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
at com.android.internal.os.PathClassLoaderFactory.createClassLoader(PathClassLoaderFactory.java:43)
提前致谢
【问题讨论】:
如果您按照here 的解释useLibrary 'org.apache.http.legacy'
会发生什么?
它给出了这个错误:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.ecoapp, PID: 16335 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/config/RequestConfig;
【参考方案1】:
用okhttp请求试试
下面是请求
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://faroo-faroo-web-search.p.rapidapi.com/api?q=food")
.get()
.addHeader("x-rapidapi-host", "faroo-faroo-web-search.p.rapidapi.com")
.addHeader("x-rapidapi-key", "f18432002emshe33f07d9b71559bp125dd7jsn2d9f1840345f")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "896be9fc-b0c5-e8eb-5f75-df68fcefab84")
.build();
Response response = client.newCall(request).execute();
然后像这样得到身体响应
String response =response.body.string()
我希望这会有所帮助,不要忘记添加 okhttp 依赖项
【讨论】:
以上是关于请求 HttpResponse<JsonNode> 时 Android 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
java使用HttpResponse请求其他系统的接口,对应Postman的设置
当对 localhost:121/logout 进行 GET 请求时,我得到“accounts.views.logout 没有返回 HttpResponse 对象”。这是一个问题吗?
Python入门自学进阶-Web框架——4HttpRequest和HttpResponse及模板