質問

I try write Rest client on Android device. Web service require custom User-Agent value. 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?

役に立ちましたか?

解決

I think you need to override getHeaders() to set the user-agent -- you are overriding 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帰属
所属していません StackOverflow
scroll top