package com.zhiwei.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONObject;

public class JsonToObject {

	public static String getToken() {
		String token = "";
		String appId = "wxd1f40dde7e15845f";
		String jmAppId = AESUtils.encrypt("wechat", appId);
		String path = "http://yuqing.zhiweidata.com/WechatPublic/common/getToken?appId=" + jmAppId;
		BufferedReader in = null;
		try {
			String result = "";
			URL url = new URL(path);
			URLConnection connection = url.openConnection();
			connection.setConnectTimeout(3000);
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result = result + line;
			}
			JSONObject jsonObject = JSONObject.fromObject(result);
			JSONObject inJson = JSONObject.fromObject(jsonObject.getString("data"));
			token = inJson.getString("accessToken");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return token;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(getToken());
	}
}
