简体   繁体   English

具有自定义http标头的Android AsyncAPI ahax查询

[英]Android AsyncAPI ahax query with custom http headers

How can I add this http header 我如何添加此http标头

contentType: "application/json; charset=utf-8"

to this ajax query? 这个ajax查询?

 public void asyncJson(){

    //perform a Google search in just a few lines of code

    String url = "http://www.mysite.com/Services/GetJson";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("nNumResults", "100");                                                  

    aq.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject json, AjaxStatus status) {

            if(json != null){

                //successful ajax call, show status code and json content
                Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();            
            }else{                    
                //ajax error, show error code
                Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
            }

        }
    });

 } // asyncJson    

I've not got a huge amount of experience with android-query but this looks like this example from their async API documentation should do the trick; 我没有大量使用android-query的经验,但这似乎来自他们的异步API文档中的示例;

String url = "http://www.mysite.com/Services/GetJson";

AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>();        
cb.url(url).type(JSONObject.class).weakHandler(this, "jsonCb");

cb.header("Content-Type", "application/json; charset=utf-8");
cb.param("nNumResults", "100");

aq.ajax(cb);

Note that when setting params you can also use a Map; 请注意,设置参数时,您还可以使用Map;

Map<String,String> params = new HashMap<>();
cb.params(params);

Watchout for the gotcha when working with the param method. WATCHOUT的疑难杂症与帕拉姆方法工作时。 It will default the "Content-Type" header for you if you haven't already set it. 如果尚未设置,它将为您默认设置“ Content-Type”标题。

You should then get back a response to this.jsonCb(String url, JSONObject jsonObject, AjaxStatus status) . 然后,您应该返回对this.jsonCb(String url, JSONObject jsonObject, AjaxStatus status)的响应。

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

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