【问题标题】:How to access api.openrouteservice.org through Androids Volley package如何通过 Androids Volley 包访问 api.openrouteservice.org
【发布时间】:2019-11-05 13:32:10
【问题描述】:

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

我的问题是我似乎错误地访问了 API,因为如果我按如下所示记录 URL 并将其从调试窗口复制到我的浏览器中,它会发送请求、获得响应并在浏览器窗口中显示它。 但是我的应用程序没有收到来自 API 的响应,因为 onResponse 代码位从未执行过,并且“获取完成”日志从未出现在实际的调试日志中。 以下是我的代码设置,它使用 Volley 访问 HTTP 请求,并且适用于其他 API。

    @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;
    }

【问题讨论】:

  • 我也是 Volley 的新手,我猜你忘记将请求添加到 RequestQueue `object.
  • 天哪,我真笨。

标签: android-studio request android-volley


【解决方案1】:

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

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

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

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

【讨论】:

  • 有一些网上的名声先生,你对我缺乏脑容量的精确知识令人震惊,我向你的扫描能力低头
猜你喜欢
  • 2013-07-05
  • 1970-01-01
  • 2016-01-01
  • 2018-10-02
  • 2012-12-15
  • 1970-01-01
  • 2016-11-30
  • 2014-02-17
  • 2021-03-05
相关资源
最近更新 更多