加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入

關(guān)于云平臺(tái)控制的實(shí)現(xiàn)

04/24 08:52
2872
服務(wù)支持:
技術(shù)交流群

完成交易后在“購買成功”頁面掃碼入群,即可與技術(shù)大咖們分享疑惑和經(jīng)驗(yàn)、收獲成長和認(rèn)同、領(lǐng)取優(yōu)惠和紅包等。

虛擬商品不可退

當(dāng)前內(nèi)容為數(shù)字版權(quán)作品,購買后不支持退換且無法轉(zhuǎn)移使用。

加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論
放大
實(shí)物圖
相關(guān)方案
  • 方案介紹
    • 平臺(tái)選擇
    • 接入云端數(shù)據(jù):
    • 聯(lián)系方式 微信號:13648103287
  • 相關(guān)文件
  • 推薦器件
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

平臺(tái)選擇

Android實(shí)現(xiàn)平臺(tái):Android student 4.2.2

硬件選擇:Keil (芯片為Stm32F103ZET6)

移動(dòng)云(ONENET):有清晰的文檔使用,結(jié)構(gòu)簡單,適合快速學(xué)習(xí)與集成自己項(xiàng)目


接入云端數(shù)據(jù):

Android端實(shí)現(xiàn):

步驟一:導(dǎo)入網(wǎng)絡(luò)請求包

    /*okhttp 插件*/
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
    /*Retrofit庫*/
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    /*Retrofit嵌套請求 rejava3*/
    implementation "io.reactivex.rxjava2:rxjava:2.1.0" // 必要rxjava2依賴
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 
    // 必要依賴,和Rxjava結(jié)合必須用到,下面會(huì)提到
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'

導(dǎo)入包說明

Okhttp3基本使用

Retrofit2 詳解和使用

使用Retrofit+RxJava實(shí)現(xiàn)網(wǎng)絡(luò)請求

步驟二:代碼實(shí)現(xiàn)

運(yùn)用 RXjava 實(shí)現(xiàn)網(wǎng)絡(luò)請求

 /**
     * TODO RX思維
     *  獲取設(shè)備狀態(tài)
     */
    public void RxAsync(){

        //步驟4:創(chuàng)建Retrofit對象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.heclouds.com/") // 設(shè)置 網(wǎng)絡(luò)請求 Url
                .addConverterFactory(GsonConverterFactory.create()) //設(shè)置使用Gson解析(記得加入依賴)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        // 步驟5:創(chuàng)建 網(wǎng)絡(luò)請求接口 的實(shí)例
        SB_HttpBinService request = retrofit.create(SB_HttpBinService.class);
        // 步驟6:采用Observable<...>形式 對 網(wǎng)絡(luò)請求 進(jìn)行封裝
        Observable<Online_json> observable = request.getMessage(State.ApiKey,State.DeviceIDs);
        System.out.println(State.DeviceIDs);
        // 步驟7:發(fā)送網(wǎng)絡(luò)請求
        observable.subscribeOn(Schedulers.io())               // 在IO線程進(jìn)行網(wǎng)絡(luò)請求
                .observeOn(AndroidSchedulers.mainThread())  // 回到主線程 處理請求結(jié)果
                .subscribe(new Observer<Online_json>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d("TAG", "開始采用subscribe連接");
                    }

                    @Override
                    public void onNext(@NotNull Online_json online_json) {

                        List<Devices> devices = online_json.getData().getDevices();
                            String o;
                            if (SB_Parameter.AllSUM < online_json.getData().getTotal_count()){
                                int j;
                                j=online_json.getData().getTotal_count()-SB_Parameter.AllSUM ;

                                for (int i = 0; i < j; i++) {
                                    map = new HashMap<>();
                                    map.put("id", "請刷新");
                                    map.put("name","請刷新");
                                    map.put("online","請刷新");
                                    list.add(map);
                                }
                                SB_Parameter.AllSUM = online_json.getData().getTotal_count();
                            }

                            for (int i = 0 ;i<online_json.getData().getTotal_count();i++){
                                if (devices.get(i).getOnline()){

                                    o = "√";
                                }else {
                                    o = "×";
                                }
                                map = list.get(i);
                                map.put("id",  devices.get(i).getId());
                                map.put("name",  devices.get(i).getTitle());
                                map.put("online", o);
                            }

                        mRecyclerView.setAdapter(mRecyclerViewAdapter);
                        Delay.Delay_ms(500);
                        mRecyclerView.setAdapter(mRecyclerViewAdapter);

                    }


                    @Override
                    public void onError(Throwable e) {
                        Log.d("TAG," ,"請求失敗");
                    }

                    @Override
                    public void onComplete() {
                        Log.d("TAG", "請求成功");
                    }
                });
    }

請求成功后的會(huì)返回服務(wù)器端返回的數(shù)據(jù)

{errno=0, data=Data{devices=[Devices{title='led1', online=false, id='735471469'}, Devices{title='DHT11', online=false, id='736585581'}], total_count=2}, error='succ'}

使用?bejson?網(wǎng)頁進(jìn)行Josn數(shù)據(jù)的解析,運(yùn)用 網(wǎng)站的?JSON生成Java實(shí)體類 功能生成實(shí)體類

在這我把實(shí)體類貼出來

類 Data
/**
  * Copyright 2021 bejson.com 
  */
package com.example.yt_app.bean.Onenet_online;
import java.util.List;

/**
 * Auto-generated: 2021-07-19 15:40:24
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Data {

    private List<Devices> devices;
    private int total_count;
    public void setDevices(List<Devices> devices) {
         this.devices = devices;
     }
     public List<Devices> getDevices() {
         return devices;
     }

    public void setTotal_count(int total_count) {
         this.total_count = total_count;
     }
     public int getTotal_count() {
         return total_count;
     }

    @Override
    public String toString() {
        return "Data{" +
                "devices=" + devices +
                ", total_count=" + total_count +
                '}';
    }
}
類 Devices
/**
  * Copyright 2021 bejson.com 
  */
package com.example.yt_app.bean.Onenet_online;

/**
 * Auto-generated: 2021-07-19 15:40:24
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Devices {

    private String title;
    private boolean online;
    private String id;
    public void setTitle(String title) {
         this.title = title;
     }
     public String getTitle() {
         return title;
     }

    public void setOnline(boolean online) {
         this.online = online;
     }
     public boolean getOnline() {
         return online;
     }

    public void setId(String id) {
         this.id = id;
     }
     public String getId() {
         return id;
     }

    @Override
    public String toString() {
        return "Devices{" +
                "title='" + title + ''' +
                ", online=" + online +
                ", id='" + id + ''' +
                '}';
    }
}
類 Online_json
/**
  * Copyright 2021 bejson.com 
  */
package com.example.yt_app.bean.Onenet_online;

/**
 * Auto-generated: 2021-07-19 15:40:24
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class Online_json {

    private int errno;
    private Data data;
    private String error;
    public void setErrno(int errno) {
         this.errno = errno;
     }
     public int getErrno() {
         return errno;
     }

    public void setData(Data data) {
         this.data = data;
     }
     public Data getData() {
         return data;
     }

    public void setError(String error) {
         this.error = error;
     }
     public String getError() {
         return error;
     }

    @Override
    public String toString() {
        return "Online_json{" +
                "errno=" + errno +
                ", data=" + data +
                ", error='" + error + ''' +
                '}';
    }
}

三個(gè)實(shí)體類寫完之后還有做一件事情

實(shí)現(xiàn) Retrofit 的接口

接口名稱

SB_HttpBinService
/**
 * Created by dell on 2021/7/16.
 */
public interface SB_HttpBinService {

//  此處回調(diào)返回的可為任意類型Call<T>,再也不用自己去解析json數(shù)據(jù)啦?。?!
    Observable<Online_json> getMessage(@Header("api-key") String key, @Query("devIds") String ids );





}

步驟三 :進(jìn)行網(wǎng)絡(luò)請求獲取數(shù)據(jù)

實(shí)現(xiàn)后效果圖


總結(jié)

寫的很少,需要學(xué)的東西很多,要多琢磨,努力,多學(xué)

同時(shí)也使用了訊飛語音平臺(tái),高德平臺(tái)


聯(lián)系方式 微信號:13648103287

  • 聯(lián)系方式.docx

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風(fēng)險(xiǎn)等級 參考價(jià)格 更多信息
KSZ8864CNXIA-TR 1 Microchip Technology Inc DATACOM, ETHERNET TRANSCEIVER, QCC64
暫無數(shù)據(jù) 查看
KSZ8567RTXI-TR 1 Microchip Technology Inc IC ETHERNET SWITCH 7PORT 128TQFP

ECAD模型

下載ECAD模型
$10.96 查看
TLE6250GV33 1 Infineon Technologies AG Interface Circuit, 1-Trnsvr, BICMOS, PDSO8, GREEN, PLASTIC, SOP-8

ECAD模型

下載ECAD模型
$3.27 查看

相關(guān)推薦

電子產(chǎn)業(yè)圖譜

方案定制,程序設(shè)計(jì)方案、單片機(jī)程序設(shè)計(jì)與講解、APP定制開發(fā)。本公眾號致力于向讀者傳遞關(guān)于程序設(shè)計(jì)和開發(fā)的相關(guān)知識(shí),并分享一些關(guān)于軟件開發(fā)的最佳實(shí)踐。如果您有什么問題或建議,請隨時(shí)聯(lián)系我們。我們將竭誠為您服務(wù)