package com.game.webgame.network;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;

public class register {

	public static void PostJsonrequest(Map<String, String> map, String url,
			Volleyinterface inface) {
		JSONObject jsonObject = new JSONObject(map);
		System.out.println(jsonObject.toString());
		
		JsonObjectRequest JsonObject1 = new JsonObjectRequest(Method.POST, url,
				jsonObject, inface.loadinglisener(), inface.errorListener());
		JsonObject1.setShouldCache(false);
		volleymanager.getInstance().getmRequestQueue().getCache().remove(url);
		volleymanager.getInstance().getmRequestQueue().add(JsonObject1);
	}

	public static void PostJsonrequest1(String json, String url,
			Volleyinterface inface) {
		JSONObject jsonObject=null;
		try {
			if(json!=null){
				jsonObject = new JSONObject(json);
			}
			
			JsonObjectRequest JsonObject1 = new JsonObjectRequest(Method.POST, url,
					jsonObject, inface.loadinglisener(), inface.errorListener());
			JsonObject1.setShouldCache(false);
			volleymanager.getInstance().getmRequestQueue().getCache().remove(url);
			
			JsonObject1.setRetryPolicy(new DefaultRetryPolicy(
	                12000,
	                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
	                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
			
			volleymanager.getInstance().getmRequestQueue().add(JsonObject1);
			
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		//JSONObject jsonObject = new JSONObject(map);
		
	}
	
	public static void webservers(Map<String, String> map, String url,
			Volleyinterface inface) {
		JsonObjectRequest JsonObject;
		if (map != null) {
			JSONObject jsonObject = new JSONObject(map);
			JsonObject = new JsonObjectRequest(Method.POST, url, jsonObject,
					inface.loadinglisener(), inface.errorListener());
		} else {
			JsonObject = new JsonObjectRequest(Method.POST, url, null,
					inface.loadinglisener(), inface.errorListener());
		}
		JsonObject.setShouldCache(false);
		volleymanager.getInstance().getmRequestQueue().getCache().remove(url);
		volleymanager.getInstance().getmRequestQueue().add(JsonObject);

	}

	public static void Stringpostrequest(final Map<String, String> map,
			String url) {

		StringRequest string = new StringRequest(Method.POST, url,
				new Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						System.out.println(arg0);
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {

					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}
		};
		string.setShouldCache(false);
		volleymanager.getInstance().getmRequestQueue().add(string);
	}

	public static void StringGETrequest(final Map<String, String> map,
			String url,Volleyinterface1 inface) {
		
		if(map!=null){
			url = url + "?" + getParamsFromMap(map);
		}
	
		StringRequest string = new StringRequest(Method.GET, url,inface.loadinglisener(), inface.errorListener());
		string.setShouldCache(false);
		
		volleymanager.getInstance().getmRequestQueue().add(string);
	}

	public static void Getjsonrequest(Map<String, String> map, String url) {
		
		
		url = url + "?" + getParamsFromMap(map);
		System.out.println(url);

		JsonObjectRequest JsonObject = new JsonObjectRequest(Method.GET, url,
				null, new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject jsonObject1) {

						if (jsonObject1 == null) {
							System.out.println("返回值为空");
						}
						
						System.out.println(jsonObject1.toString());
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {

					}
				});
		JsonObject.setShouldCache(false);
		// Myapplication.getqueue().getCache().remove(url);
		volleymanager.getInstance().getmRequestQueue().add(JsonObject);
	}

	private static String getParamsFromMap(Map<String, String> map) {
		StringBuffer buffer = new StringBuffer();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			buffer.append(key).append("=").append(value).append("&");
		}
		buffer.deleteCharAt(buffer.length() - 1);
		return buffer.toString();
	}
}
