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

武汉武昌做网站推广/百度搜索技巧

武汉武昌做网站推广,百度搜索技巧,ps做网站设计稿,舟山建设工程信息网站之前都是使用和风API获取天气数据,也可以,但SDK会了以后,更加简单直接。而且功能也比API更强大,代码更简洁。下面是我折腾差不多一周的成果,这些会了之后,获取天气预报,显示城市,天气…

之前都是使用和风API获取天气数据,也可以,但SDK会了以后,更加简单直接。而且功能也比API更强大,代码更简洁。下面是我折腾差不多一周的成果,这些会了之后,获取天气预报,显示城市,天气图标什么的也都能简单的完成。今天先把这个发出去,过两天把其他 的做完了再发。

因为和风的天气代码和json格式不断的变,所以以下代码适用于4.8版SDK,其他版本我找到了不少,基本都不适用。因为我对JAVA几乎不懂,经过自己不断实验、摸索,最后成功。所以代码肯定不好看,很多也是抄别人的,但不是抄一个人的,所以这里表示感谢,但无法一一列举。

以下是全部详细步骤及全部代码,按照我写的来一定能成功。

一、注册和风用户:https://id.qweather.com/#/login

后面验证过程就不讲了。登录后到最下面,“开发者控制台”。

点击进入。看到下面界面。

二、创建应用获取publicID 和key(关键):

点击上图上的:马上创建一个上面的“+”号。打开下图。

 点击左右“免费开发版”后,下图:

 这个可以随便写。下一步下图:

点击Andriod SDK。下一步,下图:

 都是必埴项:上面的名称可以随便写。下面的“绑定packageName”内的东西必须与你开发的app的名称一样,据说不一样获取不到数据,我没试过。一般是这种格式:com.example.thirdversionclock 就在andriodmanifest.mxl里面,下图的位置,这个名字在申请的时候可以随便取,后面想改啥改啥,随便改。

 下一步就完成了。下面就是我们要的关键的东西:publicID 和key.

 三、(andriod stutio3.5)新建一个app,名字最好就上面填的,不要在在上图的编辑里面修改app名字。

第一步选择app基本外观,我选择的是全屏的。

 下一步,下图。上面的名称看第二行,写后面的就行了。下面第二个红框是语言,选java. 点finish,完成新建项目。

 四、重点:代码部分。

具体参照开发文档。https://dev.qweather.com/docs/android-sdk/

对于小白有点小坑,我把详细步骤写一下。

1、下载SDK文件:Android SDK配置 | 和风天气开发服务

2、添加SDK文件。把上面的文件拷贝到刚才建的那个项目文件夹下

上面位置1为项目视图,位置2显示的是项目保存的路径,默认是在C盘。在项目路径下面找到/app/libs文件夹,把下载的SDK文件拷贝进去。我这个路径是:E:\AndroidStudioProjects\Weather\app\libs,然后在andriod studio里面,上面视图,找到这个文件,并在这个文件上点击右建,弹出菜单如下,点击下面红框内,导入库。

 没导入前面没三角标:

导入后前面有三角标:

 3、配置Android Manifest 添加权限。文件位置下图:

 上面框是andriod视图,app下面的manifests下面就是那个文件,双击打开。添加下面代码:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

意义:

允许连接网络android.permission.INTERNET
访问GPS定位android.permission.ACCESS_FINE_LOCATION
访问设备状态android.permission.READ_PHONE_STATE
访问WIFI状态android.permission.ACCESS_WIFI_STATE

添加位置:

 4、引用库。内容:

implementation 'com.squareup.okhttp3:okhttp:3.12.12'
implementation 'com.google.code.gson:gson:2.6.2'

位置见下图:andriod视图,下面的build.gradle(module.app),双击打开。

 上面的代码和开发文档里面的不一样,按我的写。

其他混淆什么的我就没做了。

下面是主要代码部分:

五、重要:主要文件代码:

1、布局文件全代码:这里没什么好讲的。复制覆盖粘贴就行了。(项目名要相同,不然不行)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#0099cc"tools:context=".FullscreenActivity"><!-- The primary full-screen view. This can be replaced with whatever viewis needed to present your content, e.g. VideoView, SurfaceView,TextureView, etc. --><TextViewandroid:id="@+id/fullscreen_content"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:keepScreenOn="true"android:text="@string/dummy_content"android:textColor="#33b5e5"android:textSize="50sp"android:textStyle="bold" /><TextViewandroid:id="@+id/tv_tianqi"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="天气"android:textSize="30dp"android:layout_marginTop="30dp"android:textColor="#000"/><TextViewandroid:id="@+id/tv_wendu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="温度"android:textSize="30dp"android:layout_marginTop="70dp"android:textColor="#000"/><TextViewandroid:id="@+id/tv_fengli"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="风力"android:textSize="30dp"android:layout_marginTop="110dp"android:textColor="#000"/><TextViewandroid:id="@+id/tv_fengxiang"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="风向"android:textSize="30dp"android:layout_marginTop="150dp"android:textColor="#000"/><!-- This FrameLayout insets its children based on system windows usingandroid:fitsSystemWindows. --><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"><LinearLayoutandroid:id="@+id/fullscreen_content_controls"style="?metaButtonBarStyle"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom|center_horizontal"android:background="@color/black_overlay"android:orientation="horizontal"tools:ignore="UselessParent"><Buttonandroid:id="@+id/dummy_button"style="?metaButtonBarButtonStyle"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="@string/dummy_button" /></LinearLayout></FrameLayout></FrameLayout>

2、java文件关键代码:

 public void queryWeather(){QWeather.getWeatherNow(FullscreenActivity.this, "CN101280603", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener(){public static final String TAG="he_feng_now";@Overridepublic void onError(Throwable e) {Log.i(TAG, "onError: ", e);System.out.println("Weather Now Error:"+new Gson());}@Overridepublic void onSuccess(WeatherNowBean weatherBean) {//Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));System.out.println("获取天气成功: " + new Gson().toJson(weatherBean));//先判断返回的status是否正确,当status正确时获取数据,若status不正确,可查看status对应的Code值找到原因if (Code.OK == weatherBean.getCode()) {WeatherNowBean.NowBaseBean now = weatherBean.getNow();String tianqi=now.getText();String wendu=now.getTemp()+"℃";String fengli=now.getWindScale();String fengxiang=now.getWindDir();tv_tianqi.setText("当前天气:"+tianqi);tv_wendu.setText("当前温度:"+wendu);tv_fengxiang.setText("风向:"+fengxiang);tv_fengli.setText("风力:"+fengli+"级");} else {//在此查看返回数据失败的原因Code code = weatherBean.getCode();System.out.println("失败代码: " + code);//Log.i(TAG, "failed code: " + code);}}});};

 全部代码:因为是全屏的界面很多代码不是必须的。如果文件名相同也可以直接复制覆盖粘贴。

 HeConfig.init("你的publicID", "你的key");这一行引号里的内容要修改成你的。

package com.example.weather;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;/*** An example full-screen activity that shows and hides the system UI (i.e.* status bar and navigation/system bar) with user interaction.*/
public class FullscreenActivity extends AppCompatActivity {private TextView tv_tianqi;private TextView tv_wendu;private TextView tv_fengxiang;private TextView tv_fengli;/*** Whether or not the system UI should be auto-hidden after* {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.*/private static final boolean AUTO_HIDE = true;/*** If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after* user interaction before hiding the system UI.*/private static final int AUTO_HIDE_DELAY_MILLIS = 3000;/*** Some older devices needs a small delay between UI widget updates* and a change of the status and navigation bar.*/private static final int UI_ANIMATION_DELAY = 300;private final Handler mHideHandler = new Handler();private View mContentView;private final Runnable mHidePart2Runnable = new Runnable() {@SuppressLint("InlinedApi")@Overridepublic void run() {// Delayed removal of status and navigation bar// Note that some of these constants are new as of API 16 (Jelly Bean)// and API 19 (KitKat). It is safe to use them, as they are inlined// at compile-time and do nothing on earlier devices.mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE| View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_STABLE| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);}};private View mControlsView;private final Runnable mShowPart2Runnable = new Runnable() {@Overridepublic void run() {// Delayed display of UI elementsActionBar actionBar = getSupportActionBar();if (actionBar != null) {actionBar.show();}mControlsView.setVisibility(View.VISIBLE);}};private boolean mVisible;private final Runnable mHideRunnable = new Runnable() {@Overridepublic void run() {hide();}};/*** Touch listener to use for in-layout UI controls to delay hiding the* system UI. This is to prevent the jarring behavior of controls going away* while interacting with activity UI.*/private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {if (AUTO_HIDE) {delayedHide(AUTO_HIDE_DELAY_MILLIS);}return false;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fullscreen);mVisible = true;mControlsView = findViewById(R.id.fullscreen_content_controls);mContentView = findViewById(R.id.fullscreen_content);tv_tianqi =(TextView) findViewById(R.id.tv_tianqi);tv_wendu =(TextView) findViewById(R.id.tv_wendu);tv_fengxiang =(TextView) findViewById(R.id.tv_fengxiang);tv_fengli =(TextView) findViewById(R.id.tv_fengli);HeConfig.init("你的publicID", "你的key");HeConfig.switchToDevService();queryWeather();// Set up the user interaction to manually show or hide the system UI.mContentView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {toggle();}});// Upon interacting with UI controls, delay any scheduled hide()// operations to prevent the jarring behavior of controls going away// while interacting with the UI.findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);}@Overrideprotected void onPostCreate(Bundle savedInstanceState) {super.onPostCreate(savedInstanceState);// Trigger the initial hide() shortly after the activity has been// created, to briefly hint to the user that UI controls// are available.delayedHide(100);}private void toggle() {if (mVisible) {hide();} else {show();}}private void hide() {// Hide UI firstActionBar actionBar = getSupportActionBar();if (actionBar != null) {actionBar.hide();}mControlsView.setVisibility(View.GONE);mVisible = false;// Schedule a runnable to remove the status and navigation bar after a delaymHideHandler.removeCallbacks(mShowPart2Runnable);mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);}@SuppressLint("InlinedApi")private void show() {// Show the system barmContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);mVisible = true;// Schedule a runnable to display UI elements after a delaymHideHandler.removeCallbacks(mHidePart2Runnable);mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);}/*** Schedules a call to hide() in delay milliseconds, canceling any* previously scheduled calls.*/private void delayedHide(int delayMillis) {mHideHandler.removeCallbacks(mHideRunnable);mHideHandler.postDelayed(mHideRunnable, delayMillis);}public void queryWeather(){QWeather.getWeatherNow(FullscreenActivity.this, "CN101280603", Lang.ZH_HANS, Unit.METRIC, new QWeather.OnResultWeatherNowListener(){public static final String TAG="he_feng_now";@Overridepublic void onError(Throwable e) {Log.i(TAG, "onError: ", e);System.out.println("Weather Now Error:"+new Gson());}@Overridepublic void onSuccess(WeatherNowBean weatherBean) {//Log.i(TAG, "getWeather onSuccess: " + new Gson().toJson(weatherBean));System.out.println("获取天气成功: " + new Gson().toJson(weatherBean));//先判断返回的status是否正确,当status正确时获取数据,若status不正确,可查看status对应的Code值找到原因if (Code.OK == weatherBean.getCode()) {WeatherNowBean.NowBaseBean now = weatherBean.getNow();String tianqi=now.getText();String wendu=now.getTemp()+"℃";String fengli=now.getWindScale();String fengxiang=now.getWindDir();tv_tianqi.setText("当前天气:"+tianqi);tv_wendu.setText("当前温度:"+wendu);tv_fengxiang.setText("风向:"+fengxiang);tv_fengli.setText("风力:"+fengli+"级");} else {//在此查看返回数据失败的原因Code code = weatherBean.getCode();System.out.println("失败代码: " + code);//Log.i(TAG, "failed code: " + code);}}});};}

 完成后,虚拟运行效果:

 默认的全屏界面内容我一点都没改。

http://www.jmfq.cn/news/4738807.html

相关文章:

  • 陇西 网站开发/全球最牛的搜索引擎
  • 如何建单页网站/黄页推广引流
  • 甘肃 政府网站信息内容建设/免费发广告的网站大全
  • 狮山做网站/百度电话客服
  • 怎么做国内外网站/网络推广员工资多少钱
  • 做网站网络公司/罗湖区seo排名
  • wordpress nginx伪静态规则/网站优化助手
  • 做系统网站信息检索网站/南宁seo外包要求
  • 企业网络解决方案/重庆百度推广排名优化
  • 宁夏中卫市林业生态建设局网站/搜索引擎优化的重要性
  • 营销型网站建设总结/怎样制作网站教程
  • 刚创业 建网站/百度开户推广
  • 宁波网站设计推广服务公司/十大接单平台
  • 婚纱摄影网站模板源码/班级优化大师免费下载
  • 小型b2c网站建设费用/企业网站建设方案策划
  • 如何注册api key域名/建站优化推广
  • 和外国人做古玩生意的网站/电商seo名词解释
  • 深圳自适应网站开发公司/网站建站教程
  • 亿级别网站开发注意/关键词优化一年的收费标准
  • 会员充值网站怎么做/信息流广告加盟代理
  • 双语言网站源码/aso优化运营
  • 成营销型网站制作/推广网站都有哪些
  • 重庆网站建设与推广/站长工具的使用seo综合查询排名
  • 店铺装修设计软件/优化大师电脑版官方
  • 网络求职做阿姨哪个网站好/湖南网站优化
  • 给网站设置长尾关键词/百度里面的站长工具怎么取消
  • 兼职做任务的网站/成都seo
  • 上海专业网站制作设计/关键词查询网站
  • 贵港市住房和城乡规划建设委员会网站/什么是seo站内优化
  • 网站开发敲代码/百度一下首页网页