如何通过 Androids Volley 包访问 api.openrouteservice.org

How to access api.openrouteservice.org through Androids Volley package

我想从我的 Android 应用程序访问 openrouteservice API 上的数据 - 特别是地球上两个给定坐标之间的距离。 我已经发出请求并从另一个 API 获得了可行的响应,该响应使用与此请求尝试执行的相同代码样式将两个给定地址转换为其 latlong 坐标。它工作正常,坐标到达,我可以进一步利用它们没问题。

我的问题是我似乎错误地访问了 API,因为如果我如下所示记录 URL 并将其从调试 window 复制到我的浏览器中,它会发送请求,得到响应并在浏览器中显示它 window。 但是我的应用程序没有收到来自 API 的响应,因为永远不会执行 onResponse 代码位并且 "Fetch done" 日志永远不会出现在实际的调试日志中。 以下是我的代码设置,它使用 Volley 来访问 HTTP 请求,并且适用于其他 APIs.

    @Override
    protected String doInBackground(String... strings) {
        Log.d("Run =>","Query 3");
        String targetKoordURL = null;
        String startKoordURL = null;
        try {
            startKoordURL = startK.getString("lon").concat(",").concat(startK.getString("lat"));
            targetKoordURL = targetK.getString("lon").concat(",").concat(targetK.getString("lat"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String URLfin = "https://api.openrouteservice.org/v2/directions/driving-car?api_key=5b3ce3597851110001cf624823e587e7a80c4c6ab02af6d394585213&start="+startKoordURL+"&end="+targetKoordURL;
        Log.d("Debug =>", URLfin);

        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URLfin, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                store = response;
                Log.d("Run =>", "Fetch done!");
                continueImp();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(error instanceof TimeoutError || error instanceof NoConnectionError){
                    sideFetcherHTTPRequestStart replace = new sideFetcherHTTPRequestStart();
                    replace.execute();

                    Log.d("VOLLEY_ERROR", "Retrying on Kilometer request");
                }

                error.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("Accept", "application/json,application/geo+json,application/gpx+xml,img/png; charset=utf-8");

                return params;
            }
        };

        return null;
    }

您忘记将请求添加到请求队列中,请尝试执行以下操作:

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);

JsonObjectRequest req = new JsonObjectRequest(/*params*/);

//add above request to queue
queue.add(req);