gpt4 book ai didi

java - Android 应用程序在请求 HttpResponse 时崩溃

转载 作者:行者123 更新时间:2023-12-02 03:16:11 25 4
gpt4 key购买 nike

我目前正在使用 FAROO Web Search 开发一个新闻应用程序,但是当该应用程序使用 API 代码查找新闻时,它崩溃了。我不认为这是 API 的错,因为在 RapidApi 的测试中它工作得很好。我已经在 Android Manifest 中添加了互联网权限,并且一直在稳定连接的情况下使用它。 [![API 测试][1]][1] 如图所示,在网络中测试端点时,它获得的结果没有问题。

我还尝试添加“implementation 'org.apache.httpcomponents:httpclient:4.5'”,甚至排除某些文件,因为它们生成了更多错误。

这是主要 Activity

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)

提前致谢

最佳答案

尝试使用 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 依赖项

关于java - Android 应用程序在请求 HttpResponse<JsonNode> 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com