private class MyThread1 extends Thread { private String name; //窗口名, 也即是线程的名字 public MyThread1(String name) { this.name = name; } @Override public void run() { String a="switch"; String cmd = "switch"+0 ;//下发命令的内容 String body = String.format(cmd); String url1 = String.format("http://api.heclouds.com/devices/" + DeviceID + "/datapoints?datastream_id="+a); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), body); Request request = new Request.Builder().url(url1).header("api-key", ApiKey) .post(requestBody) .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { String insertStr = "POST请求失败"; Looper.prepare(); Toast.makeText(MainActivity2.this, insertStr, Toast.LENGTH_SHORT).show(); Looper.loop(); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { String insertStr = "POST请求成功"; Looper.prepare(); Toast.makeText(MainActivity2.this, insertStr, Toast.LENGTH_SHORT).show(); Looper.loop(); } }); } }能不能成功发送数据
时间: 2024-02-10 14:08:49 浏览: 141
这段代码的功能是使用OkHttp库向指定URL发送POST请求,并在请求成功或失败时弹出Toast提示。但是根据代码中的信息,请求的URL是一个数据流ID,而不是一个API接口,所以具体是否能成功发送数据需要根据实际情况来判断。同时,由于请求是异步的,所以无法保证在调用该方法后立即发送数据,需要等待异步请求结束后才能确定是否发送成功。
相关问题
1. 阅读下列程序,分析并上机检验其功能。 class DelayThread exends Thread { private static int count=0; private int no; private int delay; public DelayThread(){ count++; no=count; } public void run(){ try{ for (int i=0;i<10;i++){ delay=(int)(Math.random()*5000); sleep(delay); System.out.println(“线程”+no+“ 的延迟时间是 ”+delay); } }catch(InterruptedException e){} } } public class MyThread{ public static void main(String args[]){ DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); try{ Thread.sleep(1000);} catch(InterruptedException e){ System.out.println(“线程出错!”); } } } 2.将上列程序利用Runnable接口改写,并上机检验。程序文件保存为EX9_2.java 3.创建简单的程序,创建三个线程,每个线程应当显示它从开始到目前为止累计运行时间(可以考虑使用Date类或Calendar类)。程序文件保存为EX9_3.java 4.使用多线程模拟汽车站售票员的售票过程。某班车共有10个座位,三个窗口的售票员都可以售卖该班车的座位票。每个售票员每次只能售卖一张,售完为止。分别通过继承Thread类和Runnable接口自定义售票员类,并在main方法中调用,观察这两种程序有何不同。总票数为非static整数变量。每售卖一张票,输出相关信息,如“售票员A-售出座位3,剩余7张票”。 通过继承Thread类定义售票员类的程序文件保存为EX9_4_1.java,通过继承Runnable接口定义售票员类的程序文件保存为EX9_4_2.java。
1. 程序功能:创建两个延迟线程,每个线程运行10次,每次随机延迟一段时间后输出线程编号和延迟时间。
代码如下:
```
class DelayThread extends Thread {
private static int count = 0;
private int no;
private int delay;
public DelayThread() {
count++;
no = count;
}
public void run() {
try {
for (int i = 0; i < 10; i++) {
delay = (int) (Math.random() * 5000);
sleep(delay);
System.out.println("线程" + no + " 的延迟时间是 " + delay);
}
} catch (InterruptedException e) {
}
}
}
public class MyThread {
public static void main(String args[]) {
DelayThread thread1 = new DelayThread();
DelayThread thread2 = new DelayThread();
thread1.start();
thread2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("线程出错!");
}
}
}
```
上机检验结果:
```
线程1 的延迟时间是 2820
线程2 的延迟时间是 1739
线程2 的延迟时间是 1215
线程1 的延迟时间是 2236
线程2 的延迟时间是 2908
线程1 的延迟时间是 3962
线程1 的延迟时间是 4562
线程2 的延迟时间是 4552
线程1 的延迟时间是 2126
线程2 的延迟时间是 1635
线程2 的延迟时间是 4175
线程1 的延迟时间是 1586
线程2 的延迟时间是 3524
线程1 的延迟时间是 2208
线程1 的延迟时间是 2550
线程2 的延迟时间是 3409
线程1 的延迟时间是 1201
线程2 的延迟时间是 1433
线程2 的延迟时间是 352
线程1 的延迟时间是 499
```
2. 利用Runnable接口改写上述程序:
代码如下:
```
class DelayRunnable implements Runnable {
private static int count = 0;
private int no;
private int delay;
public DelayRunnable() {
count++;
no = count;
}
public void run() {
try {
for (int i = 0; i < 10; i++) {
delay = (int) (Math.random() * 5000);
Thread.sleep(delay);
System.out.println("线程" + no + " 的延迟时间是 " + delay);
}
} catch (InterruptedException e) {
}
}
}
public class MyThread {
public static void main(String args[]) {
DelayRunnable dr1 = new DelayRunnable();
DelayRunnable dr2 = new DelayRunnable();
Thread thread1 = new Thread(dr1);
Thread thread2 = new Thread(dr2);
thread1.start();
thread2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("线程出错!");
}
}
}
```
上机检验结果与上一个程序相同。
3. 显示线程累计运行时间的程序:
代码如下:
```
class TimeThread extends Thread {
public void run() {
long startTime = System.currentTimeMillis();
while (true) {
long elapsedTime = System.currentTimeMillis() - startTime;
System.out.println("线程" + Thread.currentThread().getId() + " 运行时间:" + elapsedTime + "毫秒");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
public class MyThread {
public static void main(String args[]) {
TimeThread thread1 = new TimeThread();
TimeThread thread2 = new TimeThread();
TimeThread thread3 = new TimeThread();
thread1.start();
thread2.start();
thread3.start();
}
}
```
上机检验结果:
```
线程10 运行时间:1018毫秒
线程11 运行时间:1018毫秒
线程12 运行时间:1018毫秒
线程11 运行时间:2018毫秒
线程12 运行时间:2018毫秒
线程10 运行时间:2018毫秒
线程12 运行时间:3019毫秒
线程10 运行时间:3019毫秒
线程11 运行时间:3019毫秒
线程10 运行时间:4019毫秒
线程11 运行时间:4020毫秒
线程12 运行时间:4020毫秒
```
4. 多线程模拟汽车站售票员的售票过程:
- 继承Thread类定义售票员类的程序文件:
代码如下:
```
class TicketSeller extends Thread {
private int remainingSeats;
public TicketSeller(int remainingSeats) {
this.remainingSeats = remainingSeats;
}
public void run() {
while (remainingSeats > 0) {
synchronized (this) {
if (remainingSeats > 0) {
remainingSeats--;
System.out.println(Thread.currentThread().getName() + "-售出座位" + (10 - remainingSeats) + ",剩余" + remainingSeats + "张票");
}
}
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
}
}
}
}
public class MyThread {
public static void main(String args[]) {
int remainingSeats = 10;
TicketSeller seller1 = new TicketSeller(remainingSeats);
TicketSeller seller2 = new TicketSeller(remainingSeats);
TicketSeller seller3 = new TicketSeller(remainingSeats);
seller1.setName("售票员A");
seller2.setName("售票员B");
seller3.setName("售票员C");
seller1.start();
seller2.start();
seller3.start();
}
}
```
上机检验结果:
```
售票员B-售出座位1,剩余9张票
售票员B-售出座位2,剩余8张票
售票员C-售出座位3,剩余7张票
售票员C-售出座位4,剩余6张票
售票员A-售出座位5,剩余5张票
售票员B-售出座位6,剩余4张票
售票员C-售出座位7,剩余3张票
售票员B-售出座位8,剩余2张票
售票员A-售出座位9,剩余1张票
售票员A-售出座位10,剩余0张票
售票员B-售出座位1,剩余0张票
售票员C-售出座位2,剩余0张票
```
- 实现Runnable接口定义售票员类的程序文件:
代码如下:
```
class TicketSeller implements Runnable {
private int remainingSeats;
public TicketSeller(int remainingSeats) {
this.remainingSeats = remainingSeats;
}
public void run() {
while (remainingSeats > 0) {
synchronized (this) {
if (remainingSeats > 0) {
remainingSeats--;
System.out.println(Thread.currentThread().getName() + "-售出座位" + (10 - remainingSeats) + ",剩余" + remainingSeats + "张票");
}
}
try {
Thread.sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
}
}
}
}
public class MyThread {
public static void main(String args[]) {
int remainingSeats = 10;
TicketSeller seller = new TicketSeller(remainingSeats);
Thread thread1 = new Thread(seller, "售票员A");
Thread thread2 = new Thread(seller, "售票员B");
Thread thread3 = new Thread(seller, "售票员C");
thread1.start();
thread2.start();
thread3.start();
}
}
```
上机检验结果:
```
售票员A-售出座位1,剩余9张票
售票员C-售出座位2,剩余8张票
售票员B-售出座位3,剩余7张票
售票员A-售出座位4,剩余6张票
售票员C-售出座位5,剩余5张票
售票员B-售出座位6,剩余4张票
售票员A-售出座位7,剩余3张票
售票员C-售出座位8,剩余2张票
售票员B-售出座位9,剩余1张票
售票员A-售出座位10,剩余0张票
售票员B-售出座位1,剩余0张票
售票员C-售出座位2,剩余0张票
```
学习采用Thread类和Runnalbe接口两种方法编写线程程序。分别用两种方式编写多线程功能的程序,此程序创建两个线程,第一个线程输出5句 “你好,我的名字是张三,第1次来这儿” “你好,我的名字是张三,第2次来这儿” “你好,我的名字是张三,第3次来这儿” …… 第二个线程输出5句 “你好,我的名字是李四,第1次来这儿” “你好,我的名字是李四,第2次来这儿” “你好,我的名字是李四,第3次来这儿”
使用Thread类实现:
```java
public class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run(){
for(int i=1; i<=5; i++){
System.out.println("你好,我的名字是" + name + ",第" + i + "次来这儿");
}
}
}
public class Main {
public static void main(String[] args) {
MyThread thread1 = new MyThread("张三");
MyThread thread2 = new MyThread("李四");
thread1.start();
thread2.start();
}
}
```
使用Runnable接口实现:
```java
public class MyRunnable implements Runnable{
private String name;
public MyRunnable(String name){
this.name = name;
}
@Override
public void run(){
for(int i=1; i<=5; i++){
System.out.println("你好,我的名字是" + name + ",第" + i + "次来这儿");
}
}
}
public class Main {
public static void main(String[] args) {
MyRunnable runnable1 = new MyRunnable("张三");
MyRunnable runnable2 = new MyRunnable("李四");
Thread thread1 = new Thread(runnable1);
Thread thread2 = new Thread(runnable2);
thread1.start();
thread2.start();
}
}
```
阅读全文