当前位置: 首页 > 知识库问答 >
问题:

添加标题谷歌凌空请求?

慕冠宇
2023-03-14

嗯,我是这个论坛的新手,如果你能在这方面帮助我,请。我搜索了一下,但找不到如何在截击请求中添加标题。我有这段代码,我想添加accept编码:gzip和api键。我会感谢你的帮助。代码如下:

type = "cafe";
url = "https://maps.googleapis.com/maps/api/place/search/json?location=" + Global.location + "&radius=500&types=" + type + "&sensor=true&key="+placesKey;


RequestQueue rq = Volley.newRequestQueue(context);

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, 
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

List<Review> reviews = new ArrayList<Review>();
reviews = Parsing.ParseReviews(response);
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();

}
});

rq.add(jsonRequest);

共有1个答案

堵彬彬
2023-03-14
JsonObjectRequest jsObjectRequest = new JsonObjectRequest(
            Request.Method.POST,
            url,
            jsonRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Respuesta en JSON: " + response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Error Respuesta en JSON: " + error.toString());
                }
            }
    ){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            //return super.getHeaders();
            Map<String, String> params = new HashMap<>();
            params.put("Content-Encoding", "gzip");
            return params;
        }
    };

    VolleySingleton.getInstance(context).addToRequestQueue(jsObjectRequest);

您可以在getHeaders()中添加标头。

编辑:如何在gzip中编码

    JsonObjectRequest jsObjectRequest = new JsonObjectRequest(
            /*same here*/
    ){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("Content-Encoding", "gzip");
            return params;
        }

        @Override
        public byte[] getBody() {
            try{
                return Encode.gzip(super.getBody());
            }catch(IOException e){
                Log.d(TAG, e.getMessage());
                return super.getBody();
            }
        }
    };

    VolleySingleton.getInstance(context).addToRequestQueue(jsObjectRequest);

编码GZip

public static byte[] gzip(byte[] bytes) throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = null;

    try {
        gzos = new GZIPOutputStream(baos);
        gzos.write(bytes);
    }finally {
        if (gzos != null){
            try{
                gzos.close();
            }catch (IOException ignore) {}
        }
    }

    return baos.toByteArray();
}
 类似资料:
  • 我正在app engine上构建一个web应用程序。在我的例子中,这是建立在django Nonl的基础上的,但关键是它使用的是谷歌的数据存储。 我喜欢这样一个事实,即我不需要处理复制、分片、备份等,但有一件事总是妨碍我,那就是最终的一致性,这似乎妨碍了实现一个通用的Web应用程序模式,我称之为“添加 假设我有一个项目管理应用程序。项目是它的中心模型。现在有一个网页页面,我可以在其中看到所有项目的

  • 我想将事件侦听器添加到所有标记中,我正在使用GoogleMapsforRailsGem。这是我的密码 我试图将事件侦听器添加到所有标记中,但它没有发出任何警报。如何在google maps for rails中添加事件侦听器 我有一个数据数组,每个数据包含id,lat,lng

  • 问题内容: 之前我使用模块在请求中添加标头。现在,我正在对该模块尝试相同的操作。 这是我正在使用的python请求模块:http : //pypi.python.org/pypi/requests 如何向和添加标头。说我必须在标题的每个请求中添加密钥。 问题答案: 从http://docs.python- requests.org/en/latest/user/quickstart/ 您只需要用标

  • 我跟随这篇文章将谷歌分析添加到我的django中http://www.nomadblue.com/blog/django/google-analytics-tracking-code-into-django-project/ 我添加了和到 我添加了以下内容 hellowworld/上下文处理器/谷歌分析。派克 然后,我添加了helloworld。上下文处理器。谷歌分析。google_analyti

  • 我有一个函数,以一个地名作为输入,并在谷歌地图上那个地方的lat和lng位置下降一个pin。它还使用lat和lng位置设置边界,将pin设置为Viewport。一切都很好,但旧的标记没有得到清除,而添加一个新的标记。我已经清除了标记数组,但它不起作用。这是我的代码

  • 我使用“google-maps-react”,并尝试通过点击向我的地图添加新的标记。 目前,我可以通过控制台记录特定的latLng,但似乎无法创建新的LATLING。我还没反应过来。 我的onMapClick用于查找纬度和经度。但我认为我需要将位置添加到数组中,然后使用该位置更新地图。可能是错的 Im目前的解决方案是,我只是在我的 render() 中硬编码了一些标记,数组的位置在 Marker