Skip to content

Commit

Permalink
Merge pull request #1914 from Starry02/master
Browse files Browse the repository at this point in the history
#8 #357 #补充第八次实验代码
  • Loading branch information
zengsn authored Jun 11, 2019
2 parents 2a4b708 + 8475210 commit ebb0e8f
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package edu.hzuapps.androidlabs.soft1714080902440;


import java.io.File;
import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
Expand All @@ -15,6 +20,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.Manifest;
import android.content.DialogInterface;
Expand All @@ -35,8 +41,10 @@
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import soft1714080902440.androidlabs.hzuapps.edu.soft1714080902440.R;


public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener {
Button button4;
Expand All @@ -47,15 +55,14 @@ public class MainActivity extends AppCompatActivity
Button content;
ImageView image;
Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView8=findViewById(R.id.imageView8);
imageView8.setOnClickListener(new View.OnClickListener(){
imageView8.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Soft1714080902440Activity.class);
Expand All @@ -68,12 +75,10 @@ public void onClick(View v) {
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
content=(Button)findViewById(R.id.content);
image=(ImageView)findViewById(R.id.image);

content.setOnClickListener(this);
image.setOnClickListener(this);
// 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
Expand All @@ -87,7 +92,24 @@ public void onClick(View v) {
showDialogTipUserRequestPermission();
}
}
Weather weather = new Weather();
String a = weather.printTheRes("\"high\":\"",7,2);
TextView text3=(TextView) findViewById(R.id.text3);
text3.setText(a);
String b= weather.printTheRes("\"text_night\":\"",14,2);
TextView text5=(TextView) findViewById(R.id.text5);
text5.setText(b);
String c= weather.printTheRes("\"wind_direction\":\"",18,5);
TextView text6=(TextView) findViewById(R.id.text6);
text6.setText(c);
String d= weather.printTheRes("\"wind_speed\":\"",13,2);
TextView text8=(TextView) findViewById(R.id.text8);
text8.setText(d);
}
/*
******************************
*/

// 提示用户该请求权限的弹出框
private void showDialogTipUserRequestPermission() {

Expand Down Expand Up @@ -292,7 +314,7 @@ public void handleMessage(android.os.Message msg) {
};

//异步线程下载图片
class Task extends AsyncTask<String, Integer, Void>{
class Task extends AsyncTask<String, Integer, Void> {

protected Void doInBackground(String... params) {
bitmap=GetImageInputStream((String)params[0]);
Expand Down
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;
}
}

0 comments on commit ebb0e8f

Please sign in to comment.