- 请求实例
- java_sdk
- 纯java
- curl
- php
- android
- python
String res=new ShowApiRequest("http://route.showapi.com/578-6","my_appId","my_appSecret")
.post();
System.out.println(res);
public static void main(String path[]) throws Exception {
URL u=new URL("http://route.showapi.com/578-6?showapi_appid=myappid&showapi_sign=mysecret");
InputStream in=u.openStream();
ByteArrayOutputStream out=new ByteArrayOutputStream();
try {
byte buf[]=new byte[1024];
int read = 0;
while ((read = in.read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if (in != null) {
in.close();
}
}
byte b[]=out.toByteArray( );
System.out.println(new String(b,"utf-8"));
}
//字段值为中文、空格、特殊符号的地方需要做urlencode,编码为utf-8。用js的方式就是:
//encodeURIComponent('中文')
//比如"中文"转换后就是%25E4%25B8%25AD%25E6%2596%2587
curl "http://route.showapi.com/578-6?showapi_appid=123&showapi_sign=use_my_sign&"
//md5签名方式--非简单签名
<?php
header("Content-Type:text/html;charset=UTF-8");
date_default_timezone_set("PRC");
$showapi_appid = 'xxxxxx'; //替换此值,在官网的"我的应用"中找到相关值
$showapi_secret = 'xxxxxxxxx'; //替换此值,在官网的"我的应用"中找到相关值
$paramArr = array(
'showapi_appid'=> $showapi_appid
//添加其他参数
);
//创建参数(包括签名的处理)
function createParam ($paramArr,$showapi_secret) {
$paraStr = "";
$signStr = "";
ksort($paramArr);
foreach ($paramArr as $key => $val) {
if ($key != '' && $val != '') {
$signStr .= $key.$val;
$paraStr .= $key.'='.urlencode($val).'&';
}
}
$signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
$sign = strtolower(md5($signStr));
$paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
echo "排好序的参数:".$signStr."
\r\n";
return $paraStr;
}
$param = createParam($paramArr,$showapi_secret);
$url = 'http://route.showapi.com/578-6?'.$param;
echo "请求的url:".$url."
\r\n";
$result = file_get_contents($url);
echo "返回的json数据:
\r\n";
print $result.'
\r\n';
$result = json_decode($result);
echo "
\r\n取出showapi_res_code的值:
\r\n";
print_r($result->showapi_res_code);
echo "
\r\n";
?>
//以下代码为纯java实现,并未依赖第三方框架,具体传入参数请参看接口描述详情页.
protected Handler mHandler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt = (TextView) this.findViewById(R.id.textView1);
Button myBtn = (Button) this.findViewById(R.id.button1);
myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Thread(){
//在新线程中发送网络请求
public void run() {
String appid="xxx";//要替换成自己的
String secret="xxxxxxx";//要替换成自己的
final String res=new ShowApiRequest( "http://route.showapi.com/578-6", appid, secret)
.post();
System.out.println(res);
//把返回内容通过handler对象更新到界面
mHandler.post(new Thread(){
public void run() {
txt.setText(res+" "+new Date());
}
});
}
}.start();
}
});
}
#phthon3.5
from urllib import request, parse
import json
print('send data....')
showapi_appid="xxxxxxxxxx" #替换此值
showapi_sign="xxxxxxxxxx" #替换此值
url="http://route.showapi.com/578-6"
send_data = parse.urlencode([
('showapi_appid', showapi_appid)
,('showapi_sign', showapi_sign)
])
req = request.Request(url)
with request.urlopen(req, data=send_data.encode('utf-8')) as f:
print('Status:', f.status, f.reason)
str_res= f.read().decode('utf-8')
print('str_res:',str_res)
json_res=json.dumps(str_res)
print ('json_res data is:', json_res)
返回实例
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"RowNum": "",
"MovieName": "",
"TodayBox": "",
"TodayShowCount": "",
"AvgPeople": "",
"price": "",
"Attendance": ""
}
}