简体   繁体   English

Android应用程序中的自定义User-Agent

[英]Custom User-Agent in android application

I try write Rest client on Android device. 我尝试在Android设备上编写Rest客户端。 Web service require custom User-Agent value. Web服务需要自定义User-Agent值。 I set this via: 我通过以下方式设置:

        JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>( protected Map<String, String> getParams() throws AuthFailureError {
//some code
                        final HashMap<String, String> map = new HashMap<String, String>(super.getParams());
                        map.put("User-Agent", "Custom-Agent 1.0");
                        map.put("Content-Type","application/json");
                        return map;
                    }
                };

But server receive: 但是服务器收到:

Dalvik/1.4.0 (Linux; U; Android 2.3.3; sdk Build/GRI34)

How use custom User-Agent value? 如何使用自定义User-Agent值?

I think you need to override getHeaders() to set the user-agent -- you are overriding getParams(). 我认为您需要重写getHeaders()来设置用户代理-您正在重写getParams()。 Not the same thing. 不一样的东西。

/* (non-Javadoc)
 * @see com.android.volley.Request#getHeaders()
 */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = super.getHeaders();

    if (headers == null || headers.equals(Collections.emptyMap())) {
        headers = new HashMap<String, String>();
    }

    headers.put("User-Agent", "Custom-Agent 1.0");
    // probably don't need to set the content-type here -- 
    // it should be set for you by Volley
    //headers.put("Content-Type", "application/json");

    return headers;
}

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

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