首页 > 代码库 > ======Volley======

======Volley======

 Application 中	
private RequestQueue mRequestQueue;
	private static AppController mInstance;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
	}

	/**
	 * 锁定的是本类
	 * 
	 * @return
	 */
	public static AppController getInstance() {

		return mInstance;
	}

	public RequestQueue getRequestQueue() {

		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(getApplicationContext());
		}

		return mRequestQueue;
	}

	public <T> void addToRequestQueue(Request<T> req, String tag) {
		// set the default tag if tag is empty
		req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
		getRequestQueue().add(req);
	}

	public <T> void addToRequestQueue(Request<T> req) {
		req.setTag(TAG);
		getRequestQueue().add(req);
	}

	public void cancelPendingRequests(Object tag) {
		if (mRequestQueue != null) {
			mRequestQueue.cancelAll(tag);
		}
	}

(1)请求json数据:

private void showProgressDialog() {
		if (!pDialog.isShowing())
			pDialog.show();
	}

	private void hideProgressDialog() {
		if (pDialog.isShowing())
			pDialog.hide();
	}

	private JSONObject createjson(){
		
		UserBean bean = new UserBean("4","18703427048","289dff07669d7a23de0ef88d2f7129e7");
		String str ="";
		Gson gson = new Gson();
		str = gson.toJson(bean);
		try {
			return new JSONObject(str);
		} catch (JSONException e) {
			
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * Making json object request
	 * */
	private void makeJsonObjReq() {
		showProgressDialog();
		JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
				"http://#########:9084/#######/FH/UserLogin.do", createjson(),
				new Response.Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject response) {
						Log.d(TAG, response.toString());
						msgResponse.setText(response.toString());
						hideProgressDialog();
					}
				}, new Response.ErrorListener() {

					@Override
					public void one rrorResponse(VolleyError error) {
						VolleyLog.d(TAG, "Error: " + error.getMessage());
						hideProgressDialog();
					}
				}) {

//			/**
//			 * Passing some request headers
//			 * */
//			@Override
//			public Map<String, String> getHeaders() throws AuthFailureError {
//				HashMap<String, String> headers = new HashMap<String, String>();
//				headers.put("Content-Type", "application/json");
//				return headers;
//			}
//
//			@Override
//			protected Map<String, String> getParams() {
//				Map<String, String> params = new HashMap<String, String>();
//				params.put("name", "Androidhive");
//				params.put("email", "abc@androidhive.info");
//				params.put("pass", "password123");
//

			
//			}

		};

		AppController.getInstance().addToRequestQueue(jsonObjReq,
				tag_json_obj);

	}

(2)请求String数据:

//初始化一个请求队列
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
 
//根据给定的URL新建一个请求
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
   new Response.Listener() {
    @Override
    public void onResponse(String response) {
        // 在这里处理请求得到的String类型的响应
   }
}, new Response.ErrorListener() {
    @Override
    public void one rrorResponse(VolleyError error) {
        // 在这里进行出错之后的处理
   }
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
 
Map<String, String> map = new HashMap<String, String>(); 
        map.put("params1", "value1"); 
        map.put("params2", "value2"); 
        return map
 };
// 把这个请求加入请求队列
queue.add(stringRequest);















======Volley======