我正在使用这段代码获取用户坐标,然后将坐标发送到地理编码API。我的代码在模拟器中工作正常,因为我通过模拟器控制发送坐标但是当涉及到真实设备时,我没有得到任何响应。 我已经在清单中给出了必要的权限。我已经尝试了所有必要的方法,但我没有得到积极的结果。请在这方面帮助我,我需要在几天内提交我的项目。
private void location()
{
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Intent i = new Intent("LocationUpdates");
i.putExtra("LAT", location.getLatitude() + "");
i.putExtra("LNG", location.getLongitude() + "");
Toast.makeText(getApplicationContext(), "Working", Toast.LENGTH_SHORT).show();
Log.d("LAT", location.getLatitude() + "");
Log.d("LNG", location.getLongitude() + "");
locationData = location.getLatitude() + ", " + location.getLongitude();
final double latitude = location.getLatitude();
final double longitude = location.getLongitude();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JsonObjectRequest request = new JsonObjectRequest("https://maps.googleapis.com/maps/api/geocode/json?latlng=" +(longitude)+ ","+(latitude)+"&key-AIzaSyDebBZIppRUR_r5GDNFCcK8o4qcU-Yd3bg", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address");
t.setText(address);
StringTokenizer stringTokenizer = new StringTokenizer(address,",");
road = stringTokenizer.nextToken();
city = stringTokenizer.nextToken();
country = stringTokenizer.nextToken();
updateLocation();
Intent sigin = new Intent(Discover.this, NearbyUsers.class);
startActivity(sigin);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
});
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (Build.VERSION.SDK_INT >=23 && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
==PackageManager.PERMISSION_GRANTED ){
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
}else{
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
==PackageManager.PERMISSION_GRANTED ){
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, locationListener);
}
}
}
private void updateLocation() {
progressDialog.setMessage("Finding the Location...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Constants.URL_ADDRESS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", a);
params.put("address", address);
params.put("road", road);
params.put("city", city);
params.put("country", country);
return params;
}
};
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
}