简体   繁体   English

从 newsapi 获取数据

[英]get data from newsapi

I'm trying to get news API from https://newsapi.org .我正在尝试从https://newsapi.org 获取新闻 API on IntelliJ when I call: httpURLConnection.setRequestMethod("GET");当我打电话时在 IntelliJ 上: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我得到响应及其工作,但是当我调用该方法时在 androidstudio 上出现此错误:java.io.FileNotFoundException: Z5E056C500A1C4B6A7110B50D807BADE5.

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: 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>: 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.和日志: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。 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) 2回退到使用 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: 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.AsyncT 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: at java.lang.Thread.run(Thread.Z93F7 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/System: A resource failed to call end. 25A07423FE1C889F448B33D21F46Z:919) 2022-09-02 03:13:17.211 21717-21732/th.o.th.newstest02 W/系统:资源调用结束失败。

By calling the base URL ( https://newsapi.org ) with the GET method you will fetch the website content in HTML.通过使用GET方法调用基本 URL ( https://newsapi.org ),您将获取 HTML 中的网站内容。 It's just like opening the URL in your browser.就像在浏览器中打开 URL 一样。

邮递员 API 调用

According to the Documentation , you should first get an API key to be able to send requests and fetch the news.根据文档,您应该首先获得一个 API 密钥,以便能够发送请求并获取新闻。 Aside from this, you should use the correct API address mentioned here .除此之外,您应该使用此处提到的正确 API 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM