打包com.cn.baidu;
導入java.io.BufferedReader;
導入java.io.IOException;
導入java.io.InputStream;
導入java.io.InputStreamReader; 導入java.util.List;
導入org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
導入org.apache.commons.httpclient.Header; Clorg.apache.commons.httpcliclient.HttCl. commons.httpclient.HttpException;
導入org.apache.commons.httpclient.HttpStatus;
導入org.apache.commons.httpclient.methods.GetMethod;
org.apache.commons.httpclient.methods.GetMethod;
.假期
{
/**
* @param urlAll :請求介面
* @param httpArg :參數
* @return 回傳結果
*/
public static String request(String httpArg)
{
String httpUrl = "http://www.easybots.cn/api/holiday.php";
// = "http://apis.baidu.com/xiaogg/holiday/holiday";
HttpClient httpClient = new HttpClient();
GetMethod 方法= new GetMethod(httpUrl+"?"+httpArg);
字串回應= "" ;
嘗試
{
//需要加入標題資料
List
Header header = new Header();
header.setName("apikey"); setValue("7bd4e99adcc28e337cea79191fef87fc");
headers.add(header);
httpClient.getHostConfiguration().getParams().setParameter("httpClient.getHostConfiguration().getParams().setParameter("httpClient.m." ams. RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK)
{ StreamStreamStreamStream 獲取到的資訊/發送資訊).
BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
StringBuffer resBuffer = new StringBuffer();
String resTemp = "";
while ((resTemp = br.readLine() ! append(resTemp);
}
response = resBuffer.toString();
}
}
catch (HttpException e)
{
e.printStack (HttpException e)
{
e.printStack (HttpException e)
{
e.printStack (Htt); e.printStackTrace( );
}
終於
{
method.releaseConnection();
}
回傳回應;
}
public static v.結果為1,假日回覆結果為2
String httpArg = "d="+"2016 -11-20".replaceAll("-", "");
String jsonResult = request(httpArg);
System.out. println(jsonResult);
}
}