【发布时间】:2017-07-29 19:19:14
【问题描述】:
我正在尝试构建一个应用程序,在其中我使用JSON 来获取当前的latitude 和longitude 以及address,但只要我点击button,我就会得到一个我的日志中的错误:
E/Volley:[5807] BasicNetwork.performRequest:https://maps.googleapis.com/maps/api/geocode/json?latlng=0.00.0 的意外响应代码 400
代码如下:
Gps3Activity.java
public class Gps3Activity extends AppCompatActivity {
private Button display;
private LocationManager locationManager;
private LocationListener locationListener;
private RequestQueue requestQueue;
private double lat;
private double lng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps3);
display = (Button) findViewById(R.id.displayL);
requestQueue = Volley.newRequestQueue(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new myLocationlistener();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
display.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Latitude", String.valueOf(lat));
Log.e("Longitude", String.valueOf(lng));
JsonObjectRequest request = new JsonObjectRequest("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat+","+lng +"&sensor=true", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address");
//textViewCity.setText(address);
Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
});
}
private class myLocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
}
logcat
07-30 01:18:33.287 9750-9750/com.example.shaloin.gps2 W/System.err: org.json.JSONException: Index 0 out of range [0..0)
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err: at org.json.JSONArray.get(JSONArray.java:293)
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err: at org.json.JSONArray.getJSONObject(JSONArray.java:521)
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err: at com.example.shaloin.gps2.Gps3Activity$1$1.onResponse(Gps3Activity.java:63)
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err: at com.example.shaloin.gps2.Gps3Activity$1$1.onResponse(Gps3Activity.java:58)
我也无法弄清楚为什么我将latitude 和longitude 的值设为0.0。
谁能帮忙?谢谢你:)
【问题讨论】:
标签: android json android-volley android-gps