1

My goal is to display a list of nearby restaurants using the Zomato API. I first need the JSON object to get the names of these restaurants. I've already got the API key and I know that the request URL would look like this

https://developers.zomato.com/api/v2.1/search?lat=LATITUDE&lon=LONGITUDE

From the documentation https://developers.zomato.com/documentation, it seems that I have to use something called Curl but I don't know what Curl is.

curl -X GET --header "Accept: application/json" --header "user-key: API key" "https://developers.zomato.com/api/v2.1/search?&lat=LATITUDE&lon=LONGITUDE"

Any help would be appreciated.

4

2 回答 2

2

您可以使用 Rest Client 调用带有标头和 URL 的请求。我建议使用VolleyRetrofit来完成。这是一个使用 Volley 的示例:

RequestQueue queue = Volley.newRequestQueue(this);
        String url = "https://developers.zomato.com/api/v2.1/search?&lat=27&lon=153";
        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // response
                        Log.d("Response", response.toString());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                        Log.d("ERROR", "error => " + error.toString());
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user-key", "55a1d18014dd0c0dac534c02598a3368");
                params.put("Accept", "application/json");

                return params;
            }
        };
        queue.add(postRequest);

我得到的回应是:

{"restaurants":[],"results_found":0,"results_shown":0,"results_start":0}
于 2016-09-11T13:40:35.667 回答
1

我注意到 Curl 包含--header,所以我对 URL 中的标头进行了一些研究,并在之后添加了这两行url.openConnection();

URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Accept", " application/json");
urlConnection.setRequestProperty("user-key", " "+API_KEY);

我得到了我需要的东西。

于 2016-09-11T14:12:31.230 回答