-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1914 from Starry02/master
- Loading branch information
Showing
2 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...oft1714080902440/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902440/Weather.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package edu.hzuapps.androidlabs.soft1714080902440; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.net.URLEncoder; | ||
import java.security.SignatureException; | ||
import java.util.Date; | ||
|
||
import javax.crypto.Mac; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
public class Weather { | ||
public String TIANQI_DAILY_WEATHER_URL = "https://api.seniverse.com/v3/weather/daily.json"; | ||
public String TIANQI_API_SECRET_KEY = "SIJ7QSUSpBhReocdD"; // | ||
public String TIANQI_API_USER_ID = "Pci8lsQ4vQIcp0yJZ"; // | ||
/* | ||
抓取天气数据 | ||
*/ | ||
public String loadJson (String url) { | ||
StringBuilder json = new StringBuilder(); | ||
try { | ||
URL urlObject = new URL(url); | ||
URLConnection uc = urlObject.openConnection(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); | ||
String inputLine = null; | ||
while ( (inputLine = in.readLine()) != null) { | ||
json.append(inputLine); | ||
} | ||
in.close(); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return json.toString(); | ||
} | ||
/* | ||
截取字符串过程 | ||
*/ | ||
public String subString(String str, String strStart, String strEnd) { | ||
|
||
/* 找出指定的2个字符在 该字符串里面的 位置 */ | ||
int strStartIndex = str.indexOf(strStart); | ||
int strEndIndex = str.indexOf(strEnd); | ||
|
||
/* index 为负数 即表示该字符串中 没有该字符 */ | ||
if (strStartIndex < 0) { | ||
return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串"; | ||
} | ||
if (strEndIndex < 0) { | ||
return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串"; | ||
} | ||
/* 开始截取 */ | ||
String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length()); | ||
return result; | ||
} | ||
/* | ||
取出字符串 | ||
*/ | ||
public String splitData(String str, String strStart, int num) { | ||
String tempStr; | ||
tempStr = str.substring(str.indexOf(strStart)+num); | ||
return tempStr; | ||
} | ||
/** | ||
* Generate HmacSHA1 signature with given data string and key | ||
* @param data | ||
* @param key | ||
* @return | ||
* @throws SignatureException | ||
*/ | ||
public String generateSignature(String data, String key) throws SignatureException { | ||
String result; | ||
try { | ||
// get an hmac_sha1 key from the raw key bytes | ||
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1"); | ||
// get an hmac_sha1 Mac instance and initialize with the signing key | ||
Mac mac = Mac.getInstance("HmacSHA1"); | ||
mac.init(signingKey); | ||
// compute the hmac on input data bytes | ||
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8")); | ||
result = new sun.misc.BASE64Encoder().encode(rawHmac); | ||
} | ||
catch (Exception e) { | ||
throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Generate the URL to get diary weather | ||
* @param location | ||
* @param language | ||
* @param unit | ||
* @param start | ||
* @param days | ||
* @return | ||
*/ | ||
public String generateGetDiaryWeatherURL( | ||
String location, | ||
String language, | ||
String unit, | ||
String start, | ||
String days | ||
) throws SignatureException, UnsupportedEncodingException { | ||
String timestamp = String.valueOf(new Date().getTime()); | ||
String params = "ts=" + timestamp + "&ttl=1800&uid=" + TIANQI_API_USER_ID; | ||
String signature = URLEncoder.encode(generateSignature(params, TIANQI_API_SECRET_KEY), "UTF-8"); | ||
return TIANQI_DAILY_WEATHER_URL + "?" + params + "&sig=" + signature + "&location=" + location + "&language=" + language + "&unit=" + unit + "&start=" + start + "&days=" + days; | ||
} | ||
public String printTheRes(String strStart,int num1,int num2){ | ||
Weather demo = new Weather(); | ||
String json1 = ""; | ||
String json2 = ""; | ||
try { | ||
String url = demo.generateGetDiaryWeatherURL( | ||
"huizhou", | ||
"zh-Hans", | ||
"c", | ||
"1", | ||
"1" | ||
); | ||
System.out.println("URL:" + url); | ||
json1 = loadJson(url); | ||
System.out.println(json1); | ||
} catch (Exception e) { | ||
System.out.println("Exception:" + e); | ||
} | ||
|
||
String res1,res2; | ||
res1 = splitData(json1,strStart,num1); | ||
//System.out.println(res); | ||
res2 = res1.substring(0,num2); | ||
return res2; | ||
} | ||
} |