更新接收JSON格式

zhenghaoyuan_branch
zhenghaoyuan20 2 years ago
parent a3c88e7642
commit 064735f475

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RenderSettings">
<option name="showDecorations" value="true" />
</component>
</project>

@ -69,7 +69,6 @@ public class LoginActivity extends AppCompatActivity {
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
private boolean verifyUser(String username, String password){
if (mUsers.containsKey(username) && mUsers.get(username).equals(password)) {
return true;

@ -60,6 +60,8 @@ public class SettingActivity extends AppCompatActivity {
int speedProgress = sharedPreferences.getInt("speed_progress", 0);
Speed_btn.setProgress(speedProgress);
tvProgress.setText(String.valueOf(speedProgress+10));
boolean battery_btn_state= sharedPreferences.getBoolean("battery_btn_state", false);
Battery_btn.setChecked(battery_btn_state);
photo_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -69,6 +71,15 @@ public class SettingActivity extends AppCompatActivity {
editor.apply();
}
});
Battery_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 保存Switch状态
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("battery_btn_state", isChecked);
editor.apply();
}
});
Speed_btn.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@ -80,11 +91,9 @@ public class SettingActivity extends AppCompatActivity {
} catch (UnknownHostException e) {
e.printStackTrace();
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("speed_progress", progress);
editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}

@ -35,6 +35,8 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -44,10 +46,10 @@ import java.net.InetAddress;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.os.Bundle;
import android.widget.ToggleButton;
public class VideoActivity extends AppCompatActivity {
private final static String SEND_to_IP = "192.168.39.47"; //填写接收方IP
private final static String Recieve_IP = "192.168.39.47";//填服务器IP与send_to_IP为同一个
@ -304,7 +306,6 @@ public class VideoActivity extends AppCompatActivity {
super.handleMessage(msg);
}
}
/*
* UDP线
* */
@ -375,7 +376,6 @@ public class VideoActivity extends AppCompatActivity {
// 创建 DatagramSocket 对象并发送数据报文
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
// 关闭 DatagramSocket 对象
socket.close();
} catch (IOException e) {
@ -400,7 +400,7 @@ public class VideoActivity extends AppCompatActivity {
private boolean running;
private byte[] buffer;
private static final int BUFFER_SIZE = 1024; // 设置接收缓冲区大小
private JSONObject jsonObject;
public UdpDataThread() {
buffer = new byte[BUFFER_SIZE];
}
@ -414,15 +414,17 @@ public class VideoActivity extends AppCompatActivity {
while (running) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String receivedData = new String(packet.getData(), 0, packet.getLength());
if (receivedData.equals("电量低于10%")) {
battery_warning.setText(receivedData);}else{
String receivedData = new String(packet.getData(), 0, packet.getLength(), "UTF-8");
jsonObject = new JSONObject(receivedData);
String batteryStatus = jsonObject.getString("battery");
if (batteryStatus.equals("10")) {
battery_warning.setText("电量仅剩"+batteryStatus+"%");}else{
battery_warning.setText("");
}
System.out.println("接收到数据:" + receivedData);
System.out.println("接收到数据:" + batteryStatus);
// 在这里处理收到的数据,可以根据实际需求进行解析或其他操作
}
} catch (IOException e) {
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
if (socket != null) {

@ -253,6 +253,7 @@
android:layout_width="131dp"
android:layout_height="40dp"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"
android:text=""
android:textSize="22dp"/>
</FrameLayout>

Loading…
Cancel
Save