[英]get data from newsapi
我正在嘗試從https://newsapi.org 獲取新聞 API 。 當我打電話時在 IntelliJ 上:httpURLConnection.setRequestMethod("GET"); 我得到響應及其工作,但是當我調用該方法時在 androidstudio 上出現此錯誤:java.io.FileNotFoundException: Z5E056C500A1C4B6A7110B50D807BADE5.
這是完整的代碼:
` 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();
} `
這是完整的代碼:
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;
}
}
}
和日志:2022-09-02 03:11:15.329 21717-21758/th.o.th.newstest02 I/AdrenoGLES:構建配置:SL 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>:無法打開 /sys/class/kgsl/kgsl-3d0/gpu_model 2022-09-02 03:11:15.386 21717-21758/th.o.th.newstest02 W/AdrenoUtils: ReadGpuID:218: 無法從gpu_model。 回退到使用 GSL 路徑 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 permissive2=0 2022-09- 03:11:15.436 21717-21758/th.o.th.newstest02 W/Gralloc3:不支持映射器 3.x 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:在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.Z93F725A02448B533FE1C889ZF: 022-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: 在 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: 在 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: 在 android.os.AsyncT ask$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:在 java.lang.Thread.run(Thread.Z93F7 25A07423FE1C889F448B33D21F46Z:919) 2022-09-02 03:13:17.211 21717-21732/th.o.th.newstest02 W/系統:資源調用結束失敗。
通過使用GET
方法調用基本 URL ( https://newsapi.org ),您將獲取 HTML 中的網站內容。 就像在瀏覽器中打開 URL 一樣。
根據文檔,您應該首先獲得一個 API 密鑰,以便能夠發送請求並獲取新聞。 除此之外,您應該使用此處提到的正確 API 地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.