如何解决 Android Studio 中的 HttpClient 错误

How to Solve HttpClient Error in Android Studio

完整代码

public class MainActivity extends AppCompatActivity {
private Button btnTurn;
private boolean state = false;
private BluetoothAdapter bluetoothAdapter;
public static final int REQUEST_ACCESS_COARSE_LOCATION = 1;
public String mUrl = "http://192.168.6.56/server_http/api/show.php/";
public String data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnTurn = findViewById(R.id.btnTurn);
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            stateCheck();
            Log.d("Logger", "State Check");
            handler.postDelayed(this, 1000);
        }
    }, 1000);

    btnTurn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!state){
                btnTurn.setText("Turn Off");
                state = true;
            }else{
                btnTurn.setText("Turn On");
                state = false;
            }
        }
    });
}

public void stateCheck(){
    if (state){
        if (bluetoothAdapter!=null & bluetoothAdapter.isEnabled()) {
            if(checkCoarsePermission()){
                Log.d("Logger", "Discover");
                bluetoothAdapter.startDiscovery();
            }
        }
    }
}

private boolean checkCoarsePermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION},
                REQUEST_ACCESS_COARSE_LOCATION);
        return false;
    }else {
        return true;
    }
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED));
    registerReceiver(devicesFoundReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(devicesFoundReceiver);
}

private final BroadcastReceiver devicesFoundReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action= intent.getAction();

        if(BluetoothDevice.ACTION_FOUND.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int  rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
            String RSSI = String.valueOf(rssi);
            Toast.makeText(context.getApplicationContext(),"rssi "+RSSI+" "+device.getAddress(),Toast.LENGTH_SHORT).show();
            Log.d("Logger", "Recive data "+device.getAddress());
            data = "RSSI: "+RSSI+" MAC: "+device.getAddress();
            try{
                GetText(mUrl,data);
            }catch (IOException e){
                Log.e("Logger","Error Function to send");
            }

        }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
        }else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
        }
    }
};

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode){
        case REQUEST_ACCESS_COARSE_LOCATION:
            if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this,"ALLOWED", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"Forbidden",Toast.LENGTH_SHORT).show();
            } break;
    }
}

public void post(String urll, String dataa) throws Exception {
    HttpClient client = HttpClient.newBuilder().build();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(urll))
            .POST(BodyPublishers.ofString(dataa))
            .build();

    HttpResponse<?> response = client.send(request, BodyHandlers.discarding());
    System.out.println(response.statusCode());
}
}

代码问题

错误

我想使用 HTTP 协议发送 RSSI 和 MAC 蓝牙扫描,我在 Android Studio Java 中使用方法 HttpClient 作为普通 java。但就我而言,代码会出错。我不知道为什么。我的 android 工作室中包含库和 gradle,但仍然出错。任何人都可以帮助我如何解决它?谢谢

根据您发布的屏幕截图,您似乎指向包 org.apache.http 的 classes,但从您在代码中使用的方法来看,我认为您更希望使用 java.net.http classes.

所以只需将您的导入语句修复在您的 class 之上,来自:

import org.apache.http.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.HttpRequest;
//etc.

至:

import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest;
//etc.