Rumah > Java > javaTutorial > 获取当天是否节假日

获取当天是否节假日

巴扎黑
Lepaskan: 2016-12-02 09:21:34
asal
1238 orang telah melayarinya

package com.cn.baidu; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; 
import org.apache.commons.httpclient.Header; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpException; 
import org.apache.commons.httpclient.HttpStatus; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.params.HttpMethodParams; 

public class Holiday 

/** 
* @param urlAll :请求接口 
* @param httpArg :参数 
* @return 返回结果 
*/ 
public static String request(String httpArg) 

String httpUrl = "http://www.easybots.cn/api/holiday.php"; 
// String httpUrl = "http://apis.baidu.com/xiaogg/holiday/holiday"; 
HttpClient httpClient = new HttpClient(); 
GetMethod method = new GetMethod(httpUrl+"?"+httpArg); 
String response = ""; 
try 

// 需要添加的header数据 
List

headers = new ArrayList
(); 
Header header = new Header(); 
header.setName("apikey"); 
header.setValue("7bd4e99adcc28e337cea79191fef87fc"); 
headers.add(header); 
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers); 
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); 
int statusCode = httpClient.executeMethod(method); 
if (statusCode == HttpStatus.SC_OK) 

// 获取到的回执信息 
InputStream resStream = method.getResponseBodyAsStream(); 
BufferedReader br = new BufferedReader(new InputStreamReader(resStream)); 
StringBuffer resBuffer = new StringBuffer(); 
String resTemp = ""; 
while ((resTemp = br.readLine()) != null) 

resBuffer.append(resTemp); 

response = resBuffer.toString(); 


catch (HttpException e) 

e.printStackTrace(); 

catch (IOException e) 

e.printStackTrace(); 

finally 

method.releaseConnection(); 

return response; 


public static void main(String[] args) 

// 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2 
String httpArg = "d="+"2016-11-20".replaceAll("-", ""); 
String jsonResult = request(httpArg); 
System.out.println(jsonResult); 


}

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan