Skip to content

Commit

Permalink
名称修改
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxudong committed Jun 20, 2020
1 parent 6d7fbad commit 422e093
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
52 changes: 47 additions & 5 deletions app/src/main/java/org/sheedon/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.sheedon.demo.factory.MqttClient;
import org.sheedon.mqtt.Call;
import org.sheedon.mqtt.Callback;
import org.sheedon.mqtt.Observable;
import org.sheedon.mqtt.OkMqttClient;
import org.sheedon.mqtt.Request;
import org.sheedon.mqtt.RequestBuilder;
import org.sheedon.mqtt.Response;
import org.sheedon.mqtt.ResponseBody;

Expand All @@ -19,7 +24,26 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MqttClient.getInstance();
final OkMqttClient client = MqttClient.getInstance().getClient();

Request request = new RequestBuilder()
.backName("get_manager_list")
.build();

final Observable observable = client.newObservable(request);
observable.subscribe(new Callback<Response>() {
@Override
public void onFailure(Throwable e) {
System.out.println(e.getMessage());
}

@Override
public void onResponse(Response response) {
ResponseBody body = response.body();
System.out.println(body == null?"":body.getBody());
observable.cancel();
}
});

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -32,18 +56,36 @@ public void onClick(View v) {
e.printStackTrace();
}

MqttClient.getInstance().publish(jsonObject.toString(), "get_manager_list", new Callback() {
Request request = new RequestBuilder()
.payload(jsonObject.toString())
.backName("get_manager_list")
.build();

Call call = client.newCall(request);
call.enqueue(new Callback<Response>() {
@Override
public void onFailure(Throwable e) {
System.out.println(e.getMessage());

}

@Override
public void onResponse(Response response) {
ResponseBody body = response.body();
System.out.println(body == null?"":body.getBody());

}
});

// MqttClient.getInstance().publish(jsonObject.toString(), "get_manager_list", new Callback() {
// @Override
// public void onFailure(Throwable e) {
// System.out.println(e.getMessage());
// }
//
// @Override
// public void onResponse(Response response) {
// ResponseBody body = response.body();
// System.out.println(body == null?"":body.getBody());
// }
// });
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/sheedon/demo/factory/MqttClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ private MqttClient() {
private void createClient() {
// 创建MqttClient

String clientId = "";// 设置设备编号
String serverUri = "";// 设置服务器地址
String clientId = "yhkhs20181029046";// 设置设备编号
String serverUri = "tcp://yanhang.kmdns.net:3883";// 设置服务器地址
// if (clientId == null || clientId.trim().equals(""))
// return;

Queue<SubscribeBody> subscribeBodies = new ArrayDeque<>();
subscribeBodies.add(SubscribeBody.build("", 1));// 添加需要订阅主题
subscribeBodies.add(SubscribeBody.build("yh_classify/clouds/recyclable/cmd/yhkhs20181029046", 1));// 添加需要订阅主题


if (mClient == null) {
mClient = new OkMqttClient.Builder()
.clientInfo(App.getInstance(), serverUri, clientId)
.subscribeBodies(subscribeBodies)
.baseTopic("")// 添加基础主题
.baseTopic("yh_classify/device/recyclable/data/yhkhs20181029046")// 添加基础主题
.addConverterFactory(CallbackRuleConverterFactory.create())
.callback(this).build();
}
Expand Down
4 changes: 2 additions & 2 deletions mqtt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 3
versionName "1.1.1"
versionCode 4
versionName "1.1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
1 change: 0 additions & 1 deletion mqtt/src/main/java/org/sheedon/mqtt/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down
2 changes: 1 addition & 1 deletion mqtt/src/main/java/org/sheedon/mqtt/TimeOutRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void execute() {
DelayEvent delayEvent = queue.poll();
if (delayEvent != null && delayEvent.getId() != null
&& !delayEvent.getId().equals("")) {
dispatcher.finishedByLocal(delayEvent.getId(), new TimeoutException("串口超时"));
dispatcher.finishedByLocal(delayEvent.getId(), new TimeoutException("网络超时"));
}
} while (queue.size() > 0);

Expand Down

0 comments on commit 422e093

Please sign in to comment.