一个 定时向网络访问数据 的程序--地震速报

本示例 是学习Service里实现线程的基础,以及如何应用在后台运行的Service 来访问网络 Web Service

 

实现原理:  当开始接收网络服务 (Service) ,便 由 Service 对象开始向气象局网站进行解析,并将解析的结果存储在手机的存储卡,以".log"  的文件方式存在. 每次访问气象局网络数据的时间间隔为 5 s,程序会比较这5s 内两次的地震log 文件内容是否相同,如果有新的数据,以Toast 形式通知手机

具体代码请参见 EX08_17工程 ,效果图

这里需注意更新

台湾 中央气象局地震发布网页  "http://www.cwb.gov.tw/V7/earthquake/";

1.  在程序中有一个getMethod()方法,以Http  Get 方式访问网页,虽然没有传入参数,但实际上是可以传入参数的,如下:

getMethod(uriAPI+"?id=david",  "big5");  具体代码:

/* GET 方法 */
public String getMethod(String strGetURL, String strEncoding) {
String strReturn = "";
try {
HttpURLConnection urlConnection = null;
URL url = new URL(strGetURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("User-Agent",
"Mozilla/6.0 (compatible; MSIE 6.0; Windows 2000)");
urlConnection.setRequestProperty("Content-type",
"text/html; charset=" + strEncoding);
urlConnection.connect();
InputStream htmlBODY = urlConnection.getInputStream();

if (htmlBODY != null) {
int leng = 0;
byte[] Data = new byte[100];
byte[] totalData = new byte[0];
int totalLeg = 0;
do {
leng = htmlBODY.read(Data);
if (leng > 0) {
totalLeg += leng;
byte[] temp = new byte[totalLeg];
System.arraycopy(totalData, 0, temp, 0,
totalData.length);
System.arraycopy(Data, 0, temp, totalData.length, leng);
totalData = temp;
}
} while (leng > 0);

// strReturn = new String(totalData,"UTF-8");
strReturn = new String(totalData, strEncoding);
}
} catch (Exception e) {
e.printStackTrace();
}
return strReturn;
}

 2. 如果以Http Post 方式来传递参数,可利用以下方法来访问发送Post 请求之后网页的结果,代码如下:

/**
* post 请求方法
* postUrl      post的 url 网址
* postParam    传递的表单参数
* encoding     网页编码 
*/
public String postMethod(String postUrl, String postParam, String encoding) {
String result = "";
try {
URL url = new URL(postUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
//设置Browser Agent
urlConnection.setRequestProperty("User-Agent", "Mozilla/6.0 (compatible; MSIE 6.0; Windows 2000)");
// 设置Content-type 为表单编码
urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

//传入 Post 参数
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.write(postParam.getBytes());
out.flush();
out.close();

urlConnection.connect();
InputStream htmlBody = urlConnection.getInputStream();
if(null != htmlBody) {
int leng = 0;
byte[] data = new byte[100];
byte[] totalData = new byte[0];
int totalLeg = 0;

do {
leng = htmlBody.read(data);
if(leng > 0) {
totalLeg += leng;
byte[] temp = new byte[totalLeg];
System.arraycopy(totalData, 0, temp, 0, totalData.length);
System.arraycopy(data, 0, temp, data.length,leng);
totalData = temp;
}
} while (leng > 0);
result = new String(totalData,encoding);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

本示例重点在过程, 结果可能看不到什么

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值