当前位置: 首页 > news >正文

商业网站 技术网站横幅背景图片

商业网站 技术,网站横幅背景图片,用什么软件做楼盘微网站,商丘广告推广一、引言 上个月我通过腾讯位置服务#xff0c;实现了手机定位应用的开发学习。最近在看软件书籍时#xff0c;又看到了聚合数据API方面的内容。 书上介绍了聚合数据天气预报API的应用#xff0c;不过书上的代码看得有些难受#xff0c;我到聚合数据官网#xff0c;对天气…一、引言 上个月我通过腾讯位置服务实现了手机定位应用的开发学习。最近在看软件书籍时又看到了聚合数据API方面的内容。 书上介绍了聚合数据天气预报API的应用不过书上的代码看得有些难受我到聚合数据官网对天气预报API的接口文档进行了研究感觉比书上的要简单。于是我参照官网的接口文档设计查询部分的代码UI等设计则借鉴了书上的内容完成了这个应用的开发。实现的效果如下图 二、聚合数据平台选择API 聚合数据平台提供了很多的API其中免费的API也不少。要使用该平台的API自然需要先注册用户了。 注册号用户登录后先完成实名认证否则无法使用API。我是后知后觉申请API的时候出现了要求实名认证的提示。 大体的流程在网站上有说明。 完成注册和认证后进入API页面选择免费接口 免费API接口工具大全 - 聚合数据。我们要用到的天气预报接口就在第一行。 点击天气预报进入天气预报接口页面。点击“立即申请”。这个页面里还可以获得接口的相关介绍和示例代码方便我们进行应用开发。 完成申请后就可以在“个人中心 - 数据中心 - 我的API”中看到申请到的API了。聚合数据对免费接口有限制普通会员只能申请3个。大多数免费接口每天的请求次数为50次进行开发学习还是够用了。调用API需要的Key也在这个页面里。 完成了API的申请就可以着手进行软件的设计开发了。 三、软件设计 我的天气预报应用的界面设计参考了书上的样式但书上的代码将城市名写死了我希望支持输入城市名进行查询。因此设计UI时在页面顶部添加了SearchView组件用于输入城市名称输入后按下软键盘中的搜索按钮执行获取天气预报操作。界面的中间部分放置了几个TextView组件用于显示实时天气信息界面下部使用纵向的LinearLayout布局设置该布局做了圆角处理其下放置了多个TextView组件用于显示未来5天的预报信息。有参照做起来就很快了。 逻辑代码的设计则花了一些时间主要是书上的代码看着不够简洁比官网提供的示例要复杂不少。我就没有照搬书上的内容做而是综合了书上的代码与官网示例进行的设计。其中GET请求部分的代码基本照搬了“接口文档”中提供的Java示例。有所区别的是需要添加try语句否则会报错。 官网提供了接口测试功能可以先在接口测试页面查看获取天气预报的请求详情和返回结果。方便后续的代码设计。 这个API的调用接口URI格式如下: http://apis.juhe.cn/simpleWeather/query?keykeycity%E8%8B%8F%E5%B7%9E 其中key在“个人中心 - 数据中心 - 我的API”中获取。city则是从SearchView组件中获取。我将GET请求放在了SearchView组件的监听器中执行。监听器响应搜索按钮触发获取组件中的文本内容并将其传递给获取天气预报的方法。 获取天气预报的方法我是参考了官网的java示例代码如下 package cn.juhe.test;import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors;public class JavaGet {public static void main(String[] args) throws Exception {String apiKey 你申请的key;String apiUrl http://apis.juhe.cn/simpleWeather/query;HashMapString, String map new HashMap();map.put(key, apiKey);map.put(city, 苏州);URL url new URL(String.format(apiUrl ? params(map)));BufferedReader in new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));String inputLine;StringBuffer response new StringBuffer();while ((inputLine in.readLine()) ! null) {response.append(inputLine);}in.close();System.out.println(response);}public static String params(MapString, String map) {return map.entrySet().stream().map(entry - entry.getKey() entry.getValue()).collect(Collectors.joining());} } 上述代码也是将城市名写死的且获取到的结果直接打印输出不符合我们的实际应用需求得修改但关键的代码基本是可以照搬的。 在做逻辑代码设计时我遇到了两个问题 1.一开始我的GET请求代码是放在主线程中执行的结果测试时出现了报错。网上搜索后才了解到从Android 4.0 之后不能在主线程中请求HTTP需要将GET请求放在分线程中执行。 2.在分线程中执行GET请求获取到天气预报的信息后我在分线程里更新UI的代码结果测试时又报错了。一搜Android不允许在分线程中直接修改UI界面可以使用runOnUiThread方法更新UI界面。 解决了这两个问题其它方面就很顺利了完成后测试了几次还可以。 请求次数可以在“个人中心-数据中心-我的API”找到天气预报点击“统计”按钮可以查看调用情况。 四、代码展示 最终的代码如下 1. 界面设计文件  activity_weather.xml ?xml version1.0 encodingutf-8? androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.WeatherActivityTextViewandroid:idid/tv_titleandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:text天气预报android:textSize24spandroid:textStyleboldapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /LinearLayoutandroid:idid/linearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin10dpandroid:orientationhorizontalapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/tv_titleTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_gravitycenter_verticalandroid:layout_marginEnd10dpandroid:text城市android:textSize17sp /android.widget.SearchViewandroid:idid/sv_cityNameandroid:layout_widthmatch_parentandroid:layout_height40dpandroid:backgrounddrawable/shape_round_bg_grayandroid:iconifiedByDefaulttrueandroid:imeOptionsactionSearchandroid:queryHint请输入关键字android:textColorcolor/gray_78android:textSize15sp //LinearLayoutScrollViewandroid:layout_widthmatch_parentandroid:layout_height0dpandroid:layout_marginTop10dpandroid:backgroundcolor/blue_415app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/linearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin10dpandroid:orientationvertical TextViewandroid:idid/tv_resultandroid:layout_widthmatch_parentandroid:layout_heightwrap_content /TextViewandroid:idid/tv_cityNameandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text城市名android:textColorcolor/whiteandroid:textSize24sp /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:orientationhorizontalTextViewandroid:idid/tv_realTime_tempandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text~℃android:textColorcolor/whiteandroid:textSize32spandroid:textStylebold /TextViewandroid:idid/tv_realTime_weatherandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气android:textColorcolor/whiteandroid:textSize32spandroid:textStylebold //LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:orientationhorizontalTextViewandroid:idid/tv_realTime_dirandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text风向android:textColorcolor/whiteandroid:textSize22spandroid:textStylebold /TextViewandroid:idid/tv_realTime_powandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风级android:textColorcolor/whiteandroid:textSize22spandroid:textStylebold //LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:orientationhorizontalTextViewandroid:idid/tv_realTime_humandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text湿度~android:textColorcolor/whiteandroid:textSize22spandroid:textStylebold /TextViewandroid:idid/tv_realTime_aqiandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text空气质量指数~android:textColorcolor/whiteandroid:textSize22spandroid:textStylebold //LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop20dpandroid:backgrounddrawable/radius_border_15android:orientationverticalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text5天预报android:textSize20sp /TextViewandroid:idid/tv_date1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text日期1 /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:idid/tv_temp1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text~℃ /TextViewandroid:idid/tv_weather1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气 /TextViewandroid:idid/tv_dir1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风向 //LinearLayoutTextViewandroid:idid/tv_date2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text日期2 /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:idid/tv_temp2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text~℃ /TextViewandroid:idid/tv_weather2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气 /TextViewandroid:idid/tv_dir2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风向 //LinearLayoutTextViewandroid:idid/tv_date3android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text日期3 /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:idid/tv_temp3android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text~℃ /TextViewandroid:idid/tv_weather3android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气 /TextViewandroid:idid/tv_dir3android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风向 //LinearLayoutTextViewandroid:idid/tv_date4android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text日期4 /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:idid/tv_temp4android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text~℃ /TextViewandroid:idid/tv_weather4android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气 /TextViewandroid:idid/tv_dir4android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风向 //LinearLayoutTextViewandroid:idid/tv_date5android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:layout_marginTop10dpandroid:text日期5 /LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginBottom10dpandroid:orientationhorizontalTextViewandroid:idid/tv_temp5android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text~℃ /TextViewandroid:idid/tv_weather5android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text天气 /TextViewandroid:idid/tv_dir5android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginStart10dpandroid:text风向 //LinearLayout/LinearLayout/LinearLayout/ScrollView/androidx.constraintlayout.widget.ConstraintLayout 2. 逻辑代码 WeatherActivity.java import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.util.Log; import android.widget.SearchView; import android.widget.TextView;import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; // 与官网示例不同官网是net.sf.json.JSONObjectimport java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.stream.Collectors;public class WeatherActivity extends AppCompatActivity {private final static String TAG WeatherActivity;private SearchView sv_cityName; // 搜索框private TextView tv_cityName; // 显示城市名private TextView tv_realTime_temp; // 显示实时温度private TextView tv_realTime_weather; // 显示实时天气private TextView tv_realTime_dir; // 显示实时风向private TextView tv_realTime_pow; // 显示实时风级private TextView tv_realTime_hum; // 显示实时湿度private TextView tv_realTime_aqi; // 显示空气质量指数private TextView tv_date1, tv_date2, tv_date3, tv_date4, tv_date5; // 日期private TextView tv_temp1, tv_temp2, tv_temp3, tv_temp4, tv_temp5; // 温度private TextView tv_weather1, tv_weather2, tv_weather3, tv_weather4, tv_weather5; // 天气private TextView tv_dir1, tv_dir2, tv_dir3, tv_dir4, tv_dir5; // 风向private String mCityName; // 保存用户在搜索框中输入的城市名// 天气情况查询接口地址private static final String API_URL http://apis.juhe.cn/simpleWeather/query;// 接口请求Key在聚合数据网站申请天气预报API后生成的AppKeyprivate static final String API_KEY ***********************;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_weather);sv_cityName findViewById(R.id.sv_cityName); // 城市名搜索框// 实时天气tv_cityName findViewById(R.id.tv_cityName);tv_realTime_temp findViewById(R.id.tv_realTime_temp);tv_realTime_weather findViewById(R.id.tv_realTime_weather);tv_realTime_dir findViewById(R.id.tv_realTime_dir);tv_realTime_pow findViewById(R.id.tv_realTime_pow);tv_realTime_hum findViewById(R.id.tv_realTime_hum);tv_realTime_aqi findViewById(R.id.tv_realTime_aqi);// 未来5天预报天气tv_date1 findViewById(R.id.tv_date1);tv_date2 findViewById(R.id.tv_date2);tv_date3 findViewById(R.id.tv_date3);tv_date4 findViewById(R.id.tv_date4);tv_date5 findViewById(R.id.tv_date5);tv_temp1 findViewById(R.id.tv_temp1);tv_temp2 findViewById(R.id.tv_temp2);tv_temp3 findViewById(R.id.tv_temp3);tv_temp4 findViewById(R.id.tv_temp4);tv_temp5 findViewById(R.id.tv_temp5);tv_weather1 findViewById(R.id.tv_weather1);tv_weather2 findViewById(R.id.tv_weather2);tv_weather3 findViewById(R.id.tv_weather3);tv_weather4 findViewById(R.id.tv_weather4);tv_weather5 findViewById(R.id.tv_weather5);tv_dir1 findViewById(R.id.tv_dir1);tv_dir2 findViewById(R.id.tv_dir2);tv_dir3 findViewById(R.id.tv_dir3);tv_dir4 findViewById(R.id.tv_dir4);tv_dir5 findViewById(R.id.tv_dir5);// 设置搜索框监听器sv_cityName.setOnQueryTextListener(new SearchView.OnQueryTextListener() {// 当点击搜索按钮时触发该方法Overridepublic boolean onQueryTextSubmit(String s) {sv_cityName.clearFocus(); // 移除焦点mCityName s;// Android 4.0 之后不能在主线程中请求HTTPnew Thread(() - queryWeather (mCityName)).start(); // 分线程中获取天气信息return false;}// 当搜索内容改变时触发该方法Overridepublic boolean onQueryTextChange(String s) {return false;}});}/*** 根据城市名查询天气情况** param cityName 城市名称*/private void queryWeather(String cityName) {HashMapString, String map new HashMap(); //组合参数map.put(city, cityName);map.put(key, API_KEY);String queryParams params(map);try {URL url new URL(API_URL ? queryParams);Log.d(TAG, URL url);BufferedReader in new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));String inputLine;StringBuffer response new StringBuffer(); // StringBuffer是线程安全的StringBuilder效率更高但不是线程安全的while ((inputLine in.readLine()) ! null) {response.append(inputLine);}in.close();// Log.d(TAG, 查询天气返回的结果);// Log.d(TAG, response.toString());// 将获取到的结果转换为JSONObject从中获取天气信息try {JSONObject jsonObject new JSONObject(response.toString());int error_code jsonObject.getInt(error_code);if (error_code 0) {JSONObject result jsonObject.getJSONObject(result);String city result.getString(city);// 获取实时天气数据JSONObject realtime result.getJSONObject(realtime);String temp realtime.getString(temperature); // 温度String hum realtime.getString(humidity); // 湿度String info realtime.getString(info); // 天气String dir realtime.getString(direct); // 风向String pow realtime.getString(power); // 风级String aqi realtime.getString(power); // 空气质量指数// 获取未来5天的天气数据JSONArray futureArray result.getJSONArray(future);JSONObject f1 futureArray.getJSONObject(0);String date1 f1.getString(date);String temp1 f1.getString(temperature);String weather1 f1.getString(weather);String dir1 f1.getString(direct);JSONObject f2 futureArray.getJSONObject(1);String date2 f2.getString(date);String temp2 f2.getString(temperature);String weather2 f2.getString(weather);String dir2 f2.getString(direct);JSONObject f3 futureArray.getJSONObject(2);String date3 f3.getString(date);String temp3 f3.getString(temperature);String weather3 f3.getString(weather);String dir3 f3.getString(direct);JSONObject f4 futureArray.getJSONObject(3);String date4 f4.getString(date);String temp4 f4.getString(temperature);String weather4 f4.getString(weather);String dir4 f4.getString(direct);JSONObject f5 futureArray.getJSONObject(4);String date5 f5.getString(date);String temp5 f5.getString(temperature);String weather5 f5.getString(weather);String dir5 f5.getString(direct);// 分线程不能直接修改UI界面可以使用runOnUiThread方法更新UI界面runOnUiThread(() - {tv_cityName.setText(city);// 更新实时天气tv_realTime_temp.setText(String.format(Locale.CHINESE, %s%s, temp, ℃));tv_realTime_weather.setText(info);tv_realTime_dir.setText(dir);tv_realTime_pow.setText(pow);tv_realTime_hum.setText(String.format(Locale.CHINESE, %s%s, 湿度:, hum));tv_realTime_aqi.setText(String.format(Locale.CHINESE, %s%s, 空气质量指数:, aqi));// 更新未来5天预报天气tv_date1.setText(date1);tv_temp1.setText(temp1);tv_weather1.setText(weather1);tv_dir1.setText(dir1);tv_date2.setText(date2);tv_temp2.setText(temp2);tv_weather2.setText(weather2);tv_dir2.setText(dir2);tv_date3.setText(date3);tv_temp3.setText(temp3);tv_weather3.setText(weather3);tv_dir3.setText(dir3);tv_date4.setText(date4);tv_temp4.setText(temp4);tv_weather4.setText(weather4);tv_dir4.setText(dir4);tv_date5.setText(date5);tv_temp5.setText(temp5);tv_weather5.setText(weather5);tv_dir5.setText(dir5);}); // 使用runOnUiThread更新界面} else {Log.d(TAG, 调用接口失败 jsonObject.getString(reason));}} catch (JSONException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}}/*** 将map型转为请求参数型** param map map型保存的参数*/private static String params(MapString, String map) {return map.entrySet().stream().map(entry - entry.getKey() entry.getValue()).collect(Collectors.joining());} }
http://www.hkea.cn/news/14272135/

相关文章:

  • 做招聘网站的怎么让人注册简历wordpress设置导航条
  • 做网站要钱吗?企业vi设计主要包括哪些内容
  • 情侣博客网站模板下载青岛网站定制开发
  • 自己的电脑做服务器搭建网站企业网站如何宣传
  • 没有网站怎么推广网站中文通用网址域名
  • 遵义祥云平台网站建设企业网站cms程序
  • 网站开发使用的技术有哪些自己制作头像app软件
  • python做音乐网站软件外包怎么样
  • 中国网站访问量排行可以兑换微信红包的捕鱼游戏
  • 做基网站wordpress 漏洞 利用
  • 无锡网站商城建设网站设计需要什么技术
  • 杭州老牌的网站建设新闻页面设计
  • wordpress安装脚本seo技术最新黑帽
  • 自己服务器建网站 备案创建个人网站的流程
  • 教外国人做中国菜网站成都sem优化
  • 做影视网站会侵权犯法吗顺口大气三个字公司名字
  • 学校网站建设基本流程网络营销推广的方式和特点
  • winxp下做网站苏州园区体检中心
  • 西语网站域名2345推广联盟
  • 网站建设 需求网络编程技术期末考试
  • 申请微官网的网站太原做网站
  • 广东科技网站建设阳春网站开发
  • 南京外贸网站建设公司衡水商城网站制作
  • 做群头像的网站在线智慧团建入口
  • 网站流量查询站长之家玖壹购网站是做啥子的
  • 在线设计网站源码网站开发规格
  • 小学班级活动设计方案模板深圳网站优化技巧
  • 宁乡网站建设点燃网络区域工业互联网平台
  • 企业信息公示系统 全国seo快速排名软件app
  • 礼品网站实例网站开发定制模板网站建设