package com.fss.common.commonUtils.urlUtils;

import android.content.Context;
import com.fss.common.commonUtils.basicUtils.BasicUtils;
import com.fss.common.commonUtils.logUtils.Logs;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;

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

/**
 * Created by cym on 14-4-29.
 */
public class HttpUtilsAsync {
    private static final String BASE_URL = "http://api.fss.com/1/";
    private static final int TIME_OUT = 25000;
    private AsyncHttpClient client = new AsyncHttpClient();


    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(TIME_OUT);
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(TIME_OUT);
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void get(String url, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(TIME_OUT);
        client.get(getAbsoluteUrl(url), responseHandler);
    }

    public static void getWithCookie(Context context, String url, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
        //  myCookieStore.clear();
        client.setCookieStore(myCookieStore);
        client.get(getAbsoluteUrl(url), responseHandler);
//        Iterator iterator = myCookieStore.getCookies().iterator();
//        while (iterator.hasNext()) {
//            Cookie cookie = (Cookie) iterator.next();
//            Logs.d("cookie name--" + cookie.getName());
//            Logs.d("cookie value--" + cookie.getValue());
//        }

//        BasicCookieStore basicCookieStore = new BasicCookieStore();
//
//        client.setCookieStore(basicCookieStore);
//        client.get(getAbsoluteUrl(url), responseHandler);
//        Iterator iterator = basicCookieStore.getCookies().iterator();
//        while (iterator.hasNext()) {
//            Cookie cookie = (Cookie) iterator.next();
//            Logs.d("sssss" + cookie.getName());
//            Logs.d("sssss" + cookie.getValue());
//        }


    }

    public static void getUseCookie(Context context, String url, HashMap hashMap, AsyncHttpResponseHandler responseHandler) {
        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
        if (BasicUtils.judgeNotNull(hashMap)) {
            Iterator iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                Cookie cookie = new BasicClientCookie(key.toString(), value.toString());
                myCookieStore.addCookie(cookie);
            }
        }
        AsyncHttpClient client = new AsyncHttpClient();
        client.setCookieStore(myCookieStore);
        client.get(getAbsoluteUrl(url), responseHandler);
    }

    public static void getWithCookie(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
        client.setCookieStore(myCookieStore);
        client.get(getAbsoluteUrl(url), params, responseHandler);

    }

    public static void post(String url, AsyncHttpResponseHandler responseHandler) {
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(getAbsoluteUrl(url), responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
//        return BASE_URL + relativeUrl;
        return relativeUrl;
    }

    public static String getUrlFromHashMap(String originUrl, HashMap hashMap) {
        String returnUrl = originUrl;

        if (BasicUtils.judgeNotNull(hashMap)) {
            returnUrl = returnUrl + "?";
            Iterator iterator = hashMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                returnUrl += key + "=" + value + "&";
            }
            if (returnUrl.endsWith("&")) {
                returnUrl = returnUrl.substring(0, returnUrl.length() - 1);
            }
        }
        Logs.d(returnUrl);
        return returnUrl;
    }
}

@@ -3960,17 +3960,16 @@
 ndler);%0A
-%0A
     %7D%0A%0A 
@@ -4169,24 +4169,1659 @@
 er);%0A    %7D%0A%0A
+    public static void postWithCookie(Context context, String url, AsyncHttpResponseHandler responseHandler) %7B%0A        AsyncHttpClient client = new AsyncHttpClient();%0A        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);%0A        //  myCookieStore.clear();%0A        client.setCookieStore(myCookieStore);%0A        client.post(getAbsoluteUrl(url), responseHandler);%0A    %7D%0A%0A    public static void postWithCookie(Context context, String url, RequestParams params, AsyncHttpResponseHandler responseHandler) %7B%0A        AsyncHttpClient client = new AsyncHttpClient();%0A        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);%0A        client.setCookieStore(myCookieStore);%0A        client.post(getAbsoluteUrl(url), params, responseHandler);%0A    %7D%0A%0A    public static void postUseCookie(Context context, String url, HashMap hashMap, AsyncHttpResponseHandler responseHandler) %7B%0A        PersistentCookieStore myCookieStore = new PersistentCookieStore(context);%0A        if (BasicUtils.judgeNotNull(hashMap)) %7B%0A            Iterator iterator = hashMap.entrySet().iterator();%0A            while (iterator.hasNext()) %7B%0A                Map.Entry entry = (Map.Entry) iterator.next();%0A                Object key = entry.getKey();%0A                Object value = entry.getValue();%0A                Cookie cookie = new BasicClientCookie(key.toString(), value.toString());%0A                myCookieStore.addCookie(cookie);%0A            %7D%0A        %7D%0A        AsyncHttpClient client = new AsyncHttpClient();%0A        client.setCookieStore(myCookieStore);%0A        client.post(getAbsoluteUrl(url), responseHandler);%0A    %7D%0A%0A
     private 

add post with cookie