package com.bazhuayu.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TokenUtil {
private static String token;
public static String httpToken() {
String urlstring = " http://dataapi.bazhuayu.com/token";
/* JSONObject json = null; */
try {
// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 根据地址获取请求
HttpPost request = new HttpPost(urlstring);
// 建立一个NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 添加参数
params.add(new BasicNameValuePair("username", "username"));
params.add(new BasicNameValuePair("password", "password"));
params.add(new BasicNameValuePair("grant_type", "password"));
// 设置编码
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
// 判断网络连接状态码是否正常(0--200都是正常)
if (response.getStatusLine().getStatusCode() == 200) {
token = EntityUtils.toString(entity, "utf-8");
/* JSONObject.parseObject(result); */
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return token;
}
static {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
token = httpToken();
}
}, 0, 86399000);
}
public static String getToken() {
return token;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TokenUtil {
private static String token;
public static String httpToken() {
String urlstring = " http://dataapi.bazhuayu.com/token";
/* JSONObject json = null; */
try {
// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 根据地址获取请求
HttpPost request = new HttpPost(urlstring);
// 建立一个NameValuePair数组,用于存储欲传送的参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 添加参数
params.add(new BasicNameValuePair("username", "username"));
params.add(new BasicNameValuePair("password", "password"));
params.add(new BasicNameValuePair("grant_type", "password"));
// 设置编码
request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
// 判断网络连接状态码是否正常(0--200都是正常)
if (response.getStatusLine().getStatusCode() == 200) {
token = EntityUtils.toString(entity, "utf-8");
/* JSONObject.parseObject(result); */
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return token;
}
static {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
token = httpToken();
}
}, 0, 86399000);
}
public static String getToken() {
return token;
}
}