请求获取Android Studio

问题描述 投票:0回答:1

我正在创建一个应用程序来查找客户在哪里(城市、本地化、路线等),为了解决这个问题,我需要向该网站发出请求

https://nominatim.openstreetmap.org
在android studio上用java语言,可以制作吗?

public class Player extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_client);
        Background_DoWork();

    }

    
    private void Background_DoWork() {
        ImageButton imgbtnBack = findViewById(R.id.BackActivity);
        imgbtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);


        ActivityResultLauncher<String[]> locationPermissionRequest =
                registerForActivityResult(new ActivityResultContracts
                        .RequestMultiplePermissions(), result -> {
                    Boolean fineLocationGranted = result.getOrDefault(
                            android.Manifest.permission.ACCESS_FINE_LOCATION, false);
                    Boolean coarseLocationGranted = result.getOrDefault(
                            android.Manifest.permission.ACCESS_COARSE_LOCATION, false);
                    if ((fineLocationGranted != null && fineLocationGranted) || (coarseLocationGranted != null && coarseLocationGranted)) {

                        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        List<String> providers = lm.getProviders(true);
                        Location location = null;


                        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                                return;
                        }

                        lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                            @Override
                            public void onLocationChanged(@NonNull Location location) {
                                double lat = location.getLatitude();
                                Log.d("lat: ", ""+lat);
                                double lng = location.getLongitude();
                                Log.d("lng: ", ""+lng);

                                String path = "https://nominatim.openstreetmap.org/reverse?lat=" + lat +
                                        "&lon=" + lng + "&format=json&extratags=1";


                                try {
                                    /* Make the request here*/
                                }
                                catch (Exception ex) {
                                }

                            }
                        }, null);

                    }
                });

        locationPermissionRequest.launch(new String[] {
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION
        });
    }
}

我想要一个简单的请求,以便从未来的项目中了解。

java android get
1个回答
0
投票

使用 okhttp3 进行请求 get 很简单:



public class Client extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle bundle) {
        //Toast.makeText(getApplicationContext(), "Clicado o 2", Toast.LENGTH_LONG).show();
        super.onCreate(bundle);
        setContentView(R.layout.activity_client);
        Background_DoWork();

    }

    private final int Fine_Permision_Code = 1;
    private GoogleMap myMap;
    Location location;
    FusedLocationProviderClient fs;

    private void Background_DoWork() {
        ImageButton imgbtnBack = findViewById(R.id.BackActivity);
        imgbtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);


        ActivityResultLauncher<String[]> locationPermissionRequest =
                registerForActivityResult(new ActivityResultContracts
                        .RequestMultiplePermissions(), result -> {
                    Boolean fineLocationGranted = result.getOrDefault(
                            android.Manifest.permission.ACCESS_FINE_LOCATION, false);
                    Boolean coarseLocationGranted = result.getOrDefault(
                            android.Manifest.permission.ACCESS_COARSE_LOCATION, false);
                    if ((fineLocationGranted != null && fineLocationGranted) || (coarseLocationGranted != null && coarseLocationGranted)) {

                        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        List<String> providers = lm.getProviders(true);
                        Location location = null;


                        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                                return;
                        }

                        lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                            @Override
                            public void onLocationChanged(@NonNull Location location) {
                                double lat = location.getLatitude();
                                Log.d("lat: ", ""+lat);
                                double lng = location.getLongitude();
                                Log.d("lng: ", ""+lng);

                                String path = "https://nominatim.openstreetmap.org/reverse?lat=" + lat + "&lon=" + lng + "&format=json&extratags=1";

                                try {
                                    Request rq = new Request.Builder().url(path).build();
                                    OkHttpClient client = new OkHttpClient();

                                    client.newCall(rq).enqueue(new Callback() {
                                        @Override
                                        public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                            Log.d("Error", ""+e);
                                        }

                                        @Override
                                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                                            Log.d("Message", ""+response);
                                        }
                                    });
                                }
                                catch (Exception ex) {
                                    Log.d("s", ""+ex);
                                }
                                finally {
                                    //urlConnection.disconnect();
                                }

                            }
                        }, null);

                    }
                });

        locationPermissionRequest.launch(new String[] {
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION
        });
    }
}

我建议阅读 okhttp

的文档
© www.soinside.com 2019 - 2024. All rights reserved.