程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> post請求-android Volley Post請求參數丟失

post請求-android Volley Post請求參數丟失

編輯:編程綜合問答
android Volley Post請求參數丟失

自定義的Request
package cn.flymeal.androidApp.http;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;

/**

  • 自定義網絡請求(基於Volley)
  • @author zx
  • @param
    */
    public class GsonRequest extends Request {

    private static final String PROTOCOL_CHARSET = "utf-8";
    private static final String PROTOCOL_CONTENT_TYPE = String.format(
    "application/json; charset=%s", PROTOCOL_CHARSET);

    private Gson mGson;
    private final Listener mListener;
    private Map mParams=new HashMap(); ;
    private Class mClass;
    private Map headers = new HashMap();

    public GsonRequest(int method, String url, Class cls,
    Map params, Map headers,
    Listener listener, ErrorListener errorListener) {

    super(method, url, errorListener);
    mGson = new Gson();
    mClass = cls;
    mListener = listener;
    mParams = params;
    this.headers = headers;
    

    }

    /**

    • 將返回的數據處理
      */
      @Override
      protected Response parseNetworkResponse(NetworkResponse response) {
      try {
      String jsonString = new String(response.data,
      HttpHeaderParser.parseCharset(response.headers));
      T parseJson = mGson.fromJson(jsonString, mClass);
      return Response.success(parseJson,
      HttpHeaderParser.parseCacheHeaders(response));

      } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
      }
      }

    @Override
    protected void deliverResponse(T response) {

    mListener.onResponse(response);
    

    }

    @Override
    public Map getHeaders() throws AuthFailureError {

    return headers != null ? headers : super.getHeaders();
    

    }

    public void setHeaders(Map headers) {

    this.headers = headers;
    

    }

    @Override
    public String getBodyContentType() {

    return PROTOCOL_CONTENT_TYPE;
    

    }

    @Override
    protected Map getParams() throws AuthFailureError {

    return mParams;
    

    }

}
這是分裝好的一個發送請求的方法
/**
* 基於Gson的網絡Volley請求(新接口返回數據格式Volley請求)
*
* @param method
* @param url
* @param params
* @param requestListener
*/
public void executeGsonRequest(int method, String url,
Map params, Map headers,
final RequestListener requestListener) {

    requestListener.onStart();

    request = new GsonRequest<ResponseData>(method, url, ResponseData.class,
            params, headers, new Listener<ResponseData>() {

                @Override
                public void onResponse(ResponseData response) {


                    if (response.getStatus().equals("success")) {

                        requestListener.onSuccess(response.getData());
                        Log.i("volley", response.toString());

                    } else {

                        requestListener.onMessage(response.getMsg());
                    }

                    closeDialog();
                }

            }, new ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    error.printStackTrace();
                    if (error instanceof NetworkError) {
                    } else if (error instanceof ServerError) {
                    } else if (error instanceof AuthFailureError) {
                    } else if (error instanceof ParseError) {
                    } else if (error instanceof NoConnectionError) {
                    } else if (error instanceof TimeoutError) {
                    }
                    requestListener.onError(error);
                    closeDialog();
                }
            });
    requestQueue.add(request);

}

public void checkNum(Context context,String str,RequestListener requestListener){

    UrlParamJoin urlJoin=new UrlParamJoin("mvc/order/api/mobile/submitOrder.do");
    Map<String, String> headers=new HashMap<String, String>();
    headers.put(GlobalConstant.API_VERSION, "v3");
    Map<String, String> param=new HashMap<String, String>();
    param.put("orderStr", str);
    FlymealHttpClient httpClient=new FlymealHttpClient(context);
    httpClient.executeGsonRequest(Method.POST, urlJoin.getUrl(), param, headers, requestListener);

}

這是一個業務方法 求大神指導為什麼POST請求參數沒有傳過去 ?在線等!!!1

最佳回答:


有些版本的volley只支持StringRequest,然後參數以getParams形式傳入,提交方式為GET,樓主試一下吧,應該可以

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved