• 方案介紹
    • 平臺選擇
    • 接入云端數(shù)據(jù):
    • 聯(lián)系方式 微信號:13648103287
  • 附件下載
  • 推薦器件
  • 相關(guān)推薦
申請入駐 產(chǎn)業(yè)圖譜

關(guān)于云平臺控制的實現(xiàn)

2024/04/24
2572
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

聯(lián)系方式.docx

共1個文件

平臺選擇

Android實現(xiàn)平臺:Android student 4.2.2

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

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


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

Android端實現(xiàn):

步驟一:導入網(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é)合必須用到,下面會提到
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.1'

導入包說明

Okhttp3基本使用

Retrofit2 詳解和使用

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

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

運用 RXjava 實現(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ò)請求接口 的實例
        SB_HttpBinService request = retrofit.create(SB_HttpBinService.class);
        // 步驟6:采用Observable<...>形式 對 網(wǎng)絡(luò)請求 進行封裝
        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線程進行網(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", "請求成功");
                    }
                });
    }

請求成功后的會返回服務(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)頁進行Josn數(shù)據(jù)的解析,運用 網(wǎng)站的?JSON生成Java實體類 功能生成實體類

在這我把實體類貼出來

類 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 + ''' +
                '}';
    }
}

三個實體類寫完之后還有做一件事情

實現(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 );





}

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

實現(xiàn)后效果圖


總結(jié)

寫的很少,需要學的東西很多,要多琢磨,努力,多學

同時也使用了訊飛語音平臺,高德平臺


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

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

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風險等級 參考價格 更多信息
KSZ9131RNXI 1 Microchip Technology Inc Ethernet Transceiver, PQCC48

ECAD模型

下載ECAD模型
$4.24 查看
KSZ8567STXI-TR 1 Microchip Technology Inc IC ETHERNET SWITCH 7PORT 128TQFP

ECAD模型

下載ECAD模型
$10.39 查看
NCV7344D10R2G 1 onsemi CAN FD Transceiver, High Speed, Low Power with NC, long filter time, 3000-REEL
$0.82 查看

相關(guān)推薦

方案定制

去合作
方案開發(fā)定制化,2000+方案商即時響應!

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