I'm trying to get news API from https://newsapi.org. on IntelliJ when I call : httpURLConnection.setRequestMethod("GET"); I get response and its working but on the androidstudio when I call the method I get this error : java.io.FileNotFoundException: https://newsapi.org
here is the full code:
` try {
URL url = new URL("https://newsapi.org");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = bufferedReader.readLine();
StringBuilder stringBuilder = new StringBuilder();
while (s != null) {
stringBuilder.append(s);
s = bufferedReader.readLine();
}
System.out.println(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
} `
here is the full code :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mA m = new mA();
m.execute();
}
class mA extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://newsapi.org/v2/top-headlines?q=Apple&from=2022-08-31&sortBy=popularity&apiKey=648805f67bdb4aa6a454d4f6480ae35d");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = bufferedReader.readLine();
StringBuilder stringBuilder = new StringBuilder();
while (s != null) {
stringBuilder.append(s);
s = bufferedReader.readLine();
}
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
JSONArray jsonArray = jsonObject.getJSONArray("articles");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String sss = jsonObject1.getString("title");
System.out.println(stringBuilder.toString());
} catch (MalformedURLException e) {
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
and the log: 2022-09-02 03:11:15.329 21717-21758/th.o.th.newstest02 I/AdrenoGLES: Build Config : S L 8.0.11 AArch64 2022-09-02 03:11:15.375 21717-21758/th.o.th.newstest02 I/AdrenoGLES: PFP: 0x005ff112, ME: 0x005ff066 2022-09-02 03:11:15.386 21717-21758/th.o.th.newstest02 W/AdrenoUtils: <ReadGpuID_from_sysfs:194>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model 2022-09-02 03:11:15.386 21717-21758/th.o.th.newstest02 W/AdrenoUtils: ReadGpuID:218: Failed to read chip ID from gpu_model. Fallback to use the GSL path 2022-09-02 03:11:15.375 21717-21717/th.o.th.newstest02 W/RenderThread: type=1400 audit(0.0:12012): avc: denied { search } for name="kgsl-3d0" dev="sysfs" ino=29187 scontext=u:r:untrusted_app:s0:c88,c257,c512,c768 tcontext=u:object_r:sysfs_kgsl:s0 tclass=dir permissive=0 2022-09-02 03:11:15.436 21717-21758/th.o.th.newstest02 W/Gralloc3: mapper 3.x is not supported 2022-09-02 03:11:22.310 21717-21762/th.o.th.newstest02 W/System.err: java.io.FileNotFoundException: https://newsapi.org/v2/top-headlines?q=Apple&from=2022-08-31&sortBy=popularity&apiKey=648805f67bdb4aa6a454d4f6480ae35d 2022-09-02 03:11:22.311 21717-21762/th.o.th.newstest02 W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255) 2022-09-02 03:11:22.311 21717-21762/th.o.th.newstest02 W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211) 2022-09-02 03:11:22.312 21717-21762/th.o.th.newstest02 W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30) 2022-09-02 03:11:22.312 21717-21762/th.o.th.newstest02 W/System.err: at th.o.th.newstest02.MainActivity$mA.doInBackground(MainActivity.java:41) 2022-09-02 03:11:22.313 21717-21762/th.o.th.newstest02 W/System.err: at th.o.th.newstest02.MainActivity$mA.doInBackground(MainActivity.java:30) 2022-09-02 03:11:22.313 21717-21762/th.o.th.newstest02 W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:378) 2022-09-02 03:11:22.313 21717-21762/th.o.th.newstest02 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266) 2022-09-02 03:11:22.313 21717-21762/th.o.th.newstest02 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289) 2022-09-02 03:11:22.314 21717-21762/th.o.th.newstest02 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 2022-09-02 03:11:22.314 21717-21762/th.o.th.newstest02 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 2022-09-02 03:11:22.314 21717-21762/th.o.th.newstest02 W/System.err: at java.lang.Thread.run(Thread.java:919) 2022-09-02 03:13:17.211 21717-21732/th.o.th.newstest02 W/System: A resource failed to call end.
By calling the base URL (https://newsapi.org) with the
GETmethod you will fetch the website content in HTML. It's just like opening the URL in your browser.According to the Documentation, you should first get an API key to be able to send requests and fetch the news. Aside from this, you should use the correct API address mentioned here.