平臺(tái)選擇
Android實(shí)現(xiàn)平臺(tái):Android student 4.2.2
移動(dòng)云(ONENET):有清晰的文檔使用,結(jié)構(gòu)簡(jiǎn)單,適合快速學(xué)習(xí)與集成自己項(xiàng)目
接入云端數(shù)據(jù):
Android端實(shí)現(xiàn):
步驟一:導(dǎo)入網(wǎng)絡(luò)請(qǐng)求包
/*okhttp 插件*/
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
/*Retrofit庫(kù)*/
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
/*Retrofit嵌套請(qǐng)求 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ò)請(qǐng)求
步驟二:代碼實(shí)現(xiàn)
運(yùn)用 RXjava 實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求
/**
* TODO RX思維
* 獲取設(shè)備狀態(tài)
*/
public void RxAsync(){
//步驟4:創(chuàng)建Retrofit對(duì)象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.heclouds.com/") // 設(shè)置 網(wǎng)絡(luò)請(qǐng)求 Url
.addConverterFactory(GsonConverterFactory.create()) //設(shè)置使用Gson解析(記得加入依賴)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
// 步驟5:創(chuàng)建 網(wǎng)絡(luò)請(qǐng)求接口 的實(shí)例
SB_HttpBinService request = retrofit.create(SB_HttpBinService.class);
// 步驟6:采用Observable<...>形式 對(duì) 網(wǎng)絡(luò)請(qǐng)求 進(jìn)行封裝
Observable<Online_json> observable = request.getMessage(State.ApiKey,State.DeviceIDs);
System.out.println(State.DeviceIDs);
// 步驟7:發(fā)送網(wǎng)絡(luò)請(qǐng)求
observable.subscribeOn(Schedulers.io()) // 在IO線程進(jìn)行網(wǎng)絡(luò)請(qǐng)求
.observeOn(AndroidSchedulers.mainThread()) // 回到主線程 處理請(qǐng)求結(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", "請(qǐng)刷新");
map.put("name","請(qǐng)刷新");
map.put("online","請(qǐng)刷新");
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," ,"請(qǐng)求失敗");
}
@Override
public void onComplete() {
Log.d("TAG", "請(qǐng)求成功");
}
});
}
請(qǐng)求成功后的會(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ò)請(qǐng)求獲取數(shù)據(jù)
實(shí)現(xiàn)后效果圖
總結(jié):
寫的很少,需要學(xué)的東西很多,要多琢磨,努力,多學(xué)
同時(shí)也使用了訊飛語音平臺(tái),高德平臺(tái)