Search code examples
javaandroidget

Request Get on Android Studio


I'm create a app to found where is the client (city, localization, route, ...) and to solve this i need make a request get to this website https://nominatim.openstreetmap.org on android studio with language java, its possible to make?

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
        });
    }
}

I would like a simple request get, to understand from future projects.


Solution

  • It's simple to make a request get with okhttp3:

    
    
    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
            });
        }
    }
    
    

    I recomend read the documentation of okhttp