当前位置:首页 » JAVA技术教程

Android Volley网络请求框架 实现post方法并带Map参数上传

2014-03-12 19:12 本站整理 浏览(14070)

Android Volley网络请求框架 实现post方法并带Map参数上传,有需要的朋友可以参考下。

因为自己项目要求,所以研究了一下Volley这套网络请求框架,觉得挺快的,但是发现里面的JsonObjectRequest不能传一个map参数,觉得不太方便,就自己写了一个扩展 的Request,实现post方法带map参数,返回一个jsonObject或者json数组,代码很简单,就是简单地 继承并且实现了parseNetworkResponse这个方法

JsonObjectPostRequest.java

package com.example.volleysimple;

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

import org.json.JSONException;
import org.json.JSONObject;

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;

public  class JsonObjectPostRequest extends Request<JSONObject>{
	private Map<String,String> mMap;
	private Listener<JSONObject>  mListener;
	public JsonObjectPostRequest(String url,Listener<JSONObject> listener, ErrorListener errorListener,Map map) {
		super(Request.Method.POST, url, errorListener);
		mListener=listener;
		mMap=map;
		
	}
	@Override
	protected Map<String, String> getParams() throws AuthFailureError {

		return mMap;
	}

	   @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) {
		mListener.onResponse(response);
		
	}

}

这个是返回JsonArray的

JsonObjectPostRequest.java

package com.example.volleysimple;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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;

public  class JsonArrayPostRequest extends Request<JSONArray>{
	private Map<String,String> mMap;
	private Listener<JSONArray>  mListener;
	public JsonArrayPostRequest(String url,Listener<JSONArray> listener, ErrorListener errorListener,Map map) {
		super(Request.Method.POST, url, errorListener);
		mListener=listener;
		mMap=map;
		
		// TODO Auto-generated constructor stub
	}
	@Override
	protected Map<String, String> getParams() throws AuthFailureError {
		// TODO Auto-generated method stub
		return mMap;
	}

	   @Override
	    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
	        try {
	            String jsonString =
	                new String(response.data, HttpHeaderParser.parseCharset(response.headers));
	            return Response.success(new JSONArray(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(JSONArray response) {
		mListener.onResponse(response);
		
	}

}
这个是调用的一个简单的例子activity

package com.example.volleysimple;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private TextView mTextView;
	private Map<String,String> mMap;
	private String URL="http://10.0.0.188:58003/QuotationSystem/QuotationSystem/Handler1.ashx";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTextView=(TextView) findViewById(R.id.tv_main);
		mMap=new HashMap<String,String>();
		mMap.put("version", "1");
		
		RequestQueue requestQueue=Volley.newRequestQueue(this);
		JsonObjectPostRequest jsonObjectPostRequest=new JsonObjectPostRequest(URL, new Response.Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				mTextView.setText(response.toString());
				
			}
		}, new Response.ErrorListener(){

			@Override
			public void onErrorResponse(VolleyError error) {
				Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
				
			}}, mMap);
		requestQueue.add(jsonObjectPostRequest);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}