I'm currently developing a news app with FAROO Web Search, but when the app looks for the news with the API code it crashes. I don't think it's the API's fault because in the tests in RapidApi it works just fine. I've already added the internet permission in the Android Manifest and have been using it with stable connection. [![API test][1]][1] As you can see in the image, when testing the endpoint in the web it obtains the results without problem.
I've also tried to add "implementation 'org.apache.httpcomponents:httpclient:4.5'" even excluding some files because they generated more errors.
Here´s the main 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();}
}
}
}
And the error is here
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)
Thanks in advance
Try with okhttp request
Following is the request
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();
And then get body fron response like
String response =response.body.string()
I hope that will help and dont forget to add okhttp dependency
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.