在android中使用Volley库时,数据没有发布到服务器

时间:2014-11-04 09:57:08

标签: android post android-volley

我正在尝试Post某些JsonObject使用android中的Volley库的服务器。我怀疑getparams方法没有被调用,我的数据没有被发布。任何人都可以一步一步地指导我,我会出错。我的代码如下:

          case R.id.bt_search:



         try {
            jObj.put("FromDate", "04-10-2014");
            jObj.put("Todate", "04-11-2014");
            jObj.put("BillNumber", bill);
            jObj.put("Status", "NotClosed");
            jObj.put("OrderType", "1,2,3" /* Ordertype */);
            Log.i("json data on clicking search", "" + jObj);
            makeJsonObjReq(jObj);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;


  private void makeJsonObjReq(final JSONObject jObj) {
    // TODO Auto-generated method stub
    RequestQueue rq = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Method.POST,
            "myurl",
            null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject arg0) {
                    // TODO Auto-generated method stub
                    Log.d("server Response", arg0.toString());

                    try {
                        for (int i = 0; i < arg0.length(); i++) {

                        }
                    } catch (Exception e) {

                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                    // TODO Auto-generated method stub
                    VolleyLog.d("Error msg from server",
                            "Error: " + arg0.getMessage());

                }

            }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // TODO Auto-generated method stub
            Map<String, String> params = new HashMap<String, String>();
            params.put("data", jObj.toString());

            //return params;
             return super.getParams();

        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            // TODO Auto-generated method stub
            return super.getHeaders();
        }

    };

    com.cashier.volley_helper.AppController.getInstance()
            .addToRequestQueue(jsonObjReq);
    rq.add(jsonObjReq);
};

2 个答案:

答案 0 :(得分:1)

您必须修改getParams()方法

private void makeJsonObjReq(final JSONObject jObj) {
    // TODO Auto-generated method stub
    RequestQueue rq = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Method.POST,
            "myurl",
            null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject arg0) {
                    // TODO Auto-generated method stub
                    Log.d("server Response", arg0.toString());

                    try {
                        for (int i = 0; i < arg0.length(); i++) {

                        }
                    } catch (Exception e) {

                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError arg0) {
                    // TODO Auto-generated method stub
                    VolleyLog.d("Error msg from server",
                            "Error: " + arg0.getMessage());

                }

            }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // TODO Auto-generated method stub
            Map<String, String> params = super.getParams();
             if( params==null)
             params =new HashMap<String, String>();
            params.put("data", jObj.toString());

           return params;
         }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            // TODO Auto-generated method stub
            return super.getHeaders();
        }

    };

    com.cashier.volley_helper.AppController.getInstance()
            .addToRequestQueue(jsonObjReq);
    rq.add(jsonObjReq);
}

答案 1 :(得分:1)

您可以像这样使用自定义请求类

&#13;
&#13;
public class CustomRequest extends Request<JSONObject> {

	@Override
	public Entry getCacheEntry() {
		// TODO Auto-generated method stub
		return super.getCacheEntry();
	}

	@Override
	public String getCacheKey() {
		// TODO Auto-generated method stub
		return super.getCacheKey();
	}

	private Listener<JSONObject> listener;
	private Map<String, String> params;

	public CustomRequest(String url, Map<String, String> params,
			Listener<JSONObject> reponseListener, ErrorListener errorListener) {
		super(Method.GET, url, errorListener);
		this.listener = reponseListener;
		this.params = params;
	}

	public CustomRequest(int method, String url, Map<String, String> params,
			Listener<JSONObject> reponseListener, ErrorListener errorListener) {
		super(method, url, errorListener);
		this.listener = reponseListener;
		this.params = params;
	}

	@Override
	protected Map<String, String> getParams()
			throws com.android.volley.AuthFailureError {
		System.out.println(params);
		return params;
	};

	@Override
	protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
		try {
			String jsonString = new String(response.data,
					HttpHeaderParser.parseCharset(response.headers));
			return Response.success(new JSONObject(jsonString),
					HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JSONException je) {
			return Response.error(new ParseError(je));
		}
	}

	@Override
	protected void deliverResponse(JSONObject response) {
		// TODO Auto-generated method stub
		listener.onResponse(response);
	}
}
&#13;
&#13;
&#13;

并像这样使用

&#13;
&#13;
WeakHashMap<String, String> params = new WeakHashMap<String, String>();
		params.put("product_id", DetailsFragment.product_id);
		System.out.println("Printing the Jsoparams" + params);
		CustomRequest request = new CustomRequest(Method.POST, url, params,
				this.createMyReqSuccessListener(),
				this.createMyReqErrorListener()) {

		};
		volley.addToRequestQueue(request, DETAILS, true);
&#13;
&#13;
&#13;

&#13;
&#13;
private Response.Listener<JSONObject> createMyReqSuccessListener() {
		return new Response.Listener<JSONObject>() {
			@Override
			public void onResponse(JSONObject response) {
				Log.d("response from services", response.toString());
				
//Parse the response here

				

		};
	}

	private Response.ErrorListener createMyReqErrorListener() {
		return new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				System.out.println("Printing the Error" + error);
				System.out.println(VolleyErrorHelper.getMessage(error));
			}

		};
	}
&#13;
&#13;
&#13;