package com.xixun.communicate;

/*
*/

import android.util.Log;

import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpPost;
import com.koushikdutta.async.http.AsyncHttpResponse;
import com.koushikdutta.async.http.body.JSONObjectBody;

import org.json.JSONObject;

import static android.content.ContentValues.TAG;
import static java.lang.Thread.sleep;

/**
 * Created by Administrator on 2018/08/17.
 */

public class HttpLan {
    JSONObject json0 =null;
    Object obj=new Object();
    public String getAlias() {

                AsyncHttpPost post = new AsyncHttpPost("http://127.0.0.1:2016/settings");
        try {
            Log.e(TAG, "=========== getJsonParams ==============");
            JSONObject json = new JSONObject();
            json.put("_id", "AAA");
            json.put("_type", "GetCardAlias");
            JSONObjectBody body = new JSONObjectBody(json);
            post.setBody(body);
            AsyncHttpClient.getDefaultInstance().executeString(post, new AsyncHttpClient.StringCallback() {
                @Override
                public void onCompleted(Exception e, AsyncHttpResponse res, String json) {
                    if (null != e) {
                        e.printStackTrace();
                    } else {
                        Log.e(TAG, json);
                        try {
                            json0 = new JSONObject(json);

                        } catch (Exception e0) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            int iCounter=0;
            while (json0==null) {
                sleep(100);
                iCounter++;
                if (iCounter>30) break;
            }
            if (json0!=null) {
                String strAlias = (String) json0.get("alias");
                //MyLog.i("========0=================strAlias: "+strAlias);
                byte[] bt = Base64.base64Decode(strAlias);
                //byte[] bt= Base64.decodeBase64(strAlias);
                if (bt != null) {
                    //MyLog.i("========2=================Alias: " + new String(bt));
                    return new String(bt);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
/*

    public String getReultForHttpPost(String name,String pwd) throws IOException {
        //服务器  ：服务器项目  ：servlet名称
        String path="http://127.0.0.1:2016/test/test";
        HttpPost httpPost=new HttpPost(path);
        List<NameValuePair>list=new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("name", name));
        list.add(new BasicNameValuePair("pwd", pwd));
        httpPost.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));//编者按：与HttpGet区别所在，这里是将参数用List传递

        String result="";

        HttpResponse response=new DefaultHttpClient().execute(httpPost);
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity=response.getEntity();
            result=EntityUtils.toString(entity, HTTP.UTF_8);
        }
        return result;
    }

    void Test()
    {    JSONObject obj =null;

        try {

            obj = new JSONObject();
            obj.put("_id", UUID.randomUUID());
            obj.put("_type", "GetCardAlias");
            String str_obj = obj.toString();

            Log.i("yzd", str_obj);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
*/
}
