xutl3 get post參數設置

get請求

使用xUtils的HttpUtlis進行網絡請求有GET/POST兩種方式: 
當需要帶參數請求的時候要注意:

GET 請求參數:

RequestParams params = new RequestParams(); 
params.addQueryStringParameter(“key”, value);

  public NetParams(String url, int timeOut, Map<String, Object> parameters) {
        super(ConstBuild.SERVICE_NAME + url);
        setConnectTimeout(timeOut == 0 ? 30 * 1000 : timeOut * 1000);
        addQueryStringParameter("clientOs", "1");
        long timestamp = System.currentTimeMillis() / 1000;
        Set<Map.Entry<String, Object>> set = parameters.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            this.addQueryStringParameter(entry.getKey(), entry.getValue());
        }
    }

post請求參數

RequestParams params = new RequestParams(); 
params.addBodyParameter(“key”, value);

/**
     * 發起網絡請求的訪問參數設置
     *
     * @param url        方法
     * @param parameters 參數
     * @param timeOut    訪問超時(毫秒)
     */
    public static RequestParams getNetBodyParams(String url, Map parameters, int timeOut) {
        RequestParams p = new RequestParams(ConstBuild.SERVICE_NAME + url);
        p.setConnectTimeout(timeOut == 0 ? 30 * 1000 : timeOut);
        p.setReadTimeout(2 * 60 * 1000);
        p.setAutoResume(false);
        p.setMaxRetryCount(1);
        p.setCancelFast(true);
        p.setReadTimeout(120 * 1000);
        p.addQueryStringParameter("clientOs", "1");
        Set<Map.Entry<String, Object>> set = parameters.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            p.addBodyParameter(entry.getKey(), entry.getValue());
        }
        return p;
    }

注意:如果使用post請求時,使用了get的addQueryString方法,則在參數過長時,會報以下錯誤:

netexception,socket exception,413錯誤,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章