private static final int REQUEST_TIMEOUT = 20*1000;//设置请求超时10秒钟
private static final int SO_TIMEOUT = 20*1000; //设置等待数据超时时间10秒钟
public static String encode(String URL)
{
String result = URL;
if (CommonUtil.isNotEmpty(URL))
{
try
{
result = URLEncoder.encode(URL, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
return result;
}
public static String getInfomations(String url)
{
String value = null;
try
{
HttpClient client = new DefaultHttpClient();//创建一个http客户端 .
HttpGet httpGet = new HttpGet(url);//创建一个GET请求
HttpResponse response = client.execute(httpGet);//向服务器发送请求并获取服务器返回的结果
getHttpClient(client);
HttpEntity entity = response.getEntity();
if (null != entity)
{
InputStream inputStream = entity.getContent(); //返回的结果可能放到InputStream,http Header中等。
value = ParseString.Reader(inputStream);//解析流
//Header[] headers = response.getAllHeaders();
return value;
}
}catch (Exception e){
e.printStackTrace();
}
return value;
}
public static void setValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void updateValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPut.setEntity(entity);
HttpResponse response = client.execute(httpPut);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void deleteVlaues(String url)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
HttpResponse response = client.execute(httpDelete);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void getHttpClient(HttpClient client)
{
HttpParams httpParams=client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, //REQUEST_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, //SO_TIMEOUT);
//HttpClient client = new DefaultHttpClient(httpParams);
}
private static final int SO_TIMEOUT = 20*1000; //设置等待数据超时时间10秒钟
public static String encode(String URL)
{
String result = URL;
if (CommonUtil.isNotEmpty(URL))
{
try
{
result = URLEncoder.encode(URL, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
return result;
}
public static String getInfomations(String url)
{
String value = null;
try
{
HttpClient client = new DefaultHttpClient();//创建一个http客户端 .
HttpGet httpGet = new HttpGet(url);//创建一个GET请求
HttpResponse response = client.execute(httpGet);//向服务器发送请求并获取服务器返回的结果
getHttpClient(client);
HttpEntity entity = response.getEntity();
if (null != entity)
{
InputStream inputStream = entity.getContent(); //返回的结果可能放到InputStream,http Header中等。
value = ParseString.Reader(inputStream);//解析流
//Header[] headers = response.getAllHeaders();
return value;
}
}catch (Exception e){
e.printStackTrace();
}
return value;
}
public static void setValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPost.setEntity(entity);
HttpResponse response = client.execute(httpPost);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void updateValues(String url, Object object)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
final List dataList = new ArrayList();
dataList.add(object);
HttpEntity entity = new UrlEncodedFormEntity(dataList, "UTF-8");
httpPut.setEntity(entity);
HttpResponse response = client.execute(httpPut);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void deleteVlaues(String url)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
HttpResponse response = client.execute(httpDelete);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void getHttpClient(HttpClient client)
{
HttpParams httpParams=client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, //REQUEST_TIMEOUT);
//httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, //SO_TIMEOUT);
//HttpClient client = new DefaultHttpClient(httpParams);
}