2024.5.17(第二阶段冲刺第五天)
所学时间:3小时
代码行数:71行
博客园数:1篇
所学知识:
1.我昨天的成就:把拍完的照片显示到页面上。
2.遇到什么困难:retrofit2传输到后端出现空
3.今天的任务:把显示到页面上的照片通过retrofit2传输到前端页面上
4.今天所敲的代码:
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://172.20.10.3:8080/getFish/getWuRanFish")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Gson gson = new Gson();
Type resultType = new TypeToken<Result<Integer>>() {}.getType();
Result<FishData> r = gson.fromJson(response.body().string(), resultType);
data = r.getResult();
System.out.println("Received data: " + data);
} else {
System.out.println("Request failed: " + response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
mainHandler.post(new Runnable() {
@Override
public void run() {
if(data != null)
{
String fishname=data.getFishName();
String wuranzhishu = data.getPollution_level();
String yuanyin = data.getReason();
tv_IsWuRan.setText(fishname+" 污染指数 "+ wuranzhishu+" 原因 "+yuanyin);
}else{
tv_IsWuRan.setText("没有被污染");
}
}
});
}
}).start();
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://172.20.10.3:8080/getFish/getWuRanFish")
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Gson gson = new Gson();
Type resultType = new TypeToken<Result<Integer>>() {}.getType();
Result<FishData> r = gson.fromJson(response.body().string(), resultType);
data = r.getResult();
System.out.println("Received data: " + data);
} else {
System.out.println("Request failed: " + response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
mainHandler.post(new Runnable() {
@Override
public void run() {
if(data != null)
{
String fishname=data.getFishName();
String wuranzhishu = data.getPollution_level();
String yuanyin = data.getReason();
tv_IsWuRan.setText(fishname+" 污染指数 "+ wuranzhishu+" 原因 "+yuanyin);
}else{
tv_IsWuRan.setText("没有被污染");
}
}
});
}
}).start();