BasicNetwork.performRequest:android 中的意外响应代码 400
BasicNetwork.performRequest: Unexpected response code 400 in android
我正在尝试构建一个应用程序,我使用 JSON
从中获取当前的 latitude
和 longitude
以及 address
,但一旦我单击 button
我在日志中收到错误消息:
E/Volley: [5807] BasicNetwork.performRequest: Unexpected response code 400 for https://maps.googleapis.com/maps/api/geocode/json?latlng=0.00.0
代码如下:
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.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.onResponse(Gps3Activity.java:58)
我也无法弄清楚为什么我得到 latitude
和 longitude
的值是 0.0
。
有人能帮忙吗?谢谢:)
那里似乎有 3 个问题:
第一个:
JsonObjectRequest()
的格式似乎完全错误。正确的格式是:
JsonObjectRequest(int method,
String url,
JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener)
其次:您从地理编码 API
收到意外响应
要使用 google 的地理编码 APIs,您必须首先获得 API 密钥。
格式应该类似于:(参考 this 文档)
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address&key=YOUR_API_KEY
所以你可以看到,它需要 API 键。您可以阅读 here 来获取密钥。另外 注意 纬度值和经度值之间有一个逗号 (,)。你的代码缺少那个。
第三:你得到 lat
和 lng
作为 0.0
那是因为获取位置数据需要时间。 OS 在具有一定精度的位置数据之前不会调用 onLocationChange()
。因此,当您单击该按钮时,到那时您还没有更新位置。您应该做的是,当用户单击该按钮时,向他显示一个带有消息 updating location
的进度条,并且仅在调用 onLocationChange()
时才关闭它。仅当至少调用一次 'onLocationChange()' 时才使用 lat
和 lng
。
我只需要更改 JsonObjectRequest()
的格式。根据@ZeekHuge
的建议,下面给出了正确的格式
JsonObjectRequest(int method,
String url,
JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener)
正确的代码如下:
public class Gps3Activity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
private Button display;
private TextView displayLocation;
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);
displayLocation=(TextView)findViewById(R.id.location1);
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) {
getLocation(lat,lng);
Log.e("Latitude", String.valueOf(lat));
Log.e("Longitude", String.valueOf(lng));
Log.e("city",address);
Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG);
displayLocation.setText("City : "+address);
}
});
}
private class myLocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
//Toast.makeText(getApplicationContext(),"Latitude: "+lat+"\nLongitude: "+lng,Toast.LENGTH_LONG).show();
getLocation(lat,lng);
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
public void getLocation(double latitude,double longitude){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
"http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+longitude +"&sensor=true",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
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);
}
}
虽然使用此方法显示 location
需要花费大量时间。
我正在尝试构建一个应用程序,我使用 JSON
从中获取当前的 latitude
和 longitude
以及 address
,但一旦我单击 button
我在日志中收到错误消息:
E/Volley: [5807] BasicNetwork.performRequest: Unexpected response code 400 for https://maps.googleapis.com/maps/api/geocode/json?latlng=0.00.0
代码如下:
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.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.onResponse(Gps3Activity.java:58)
我也无法弄清楚为什么我得到 latitude
和 longitude
的值是 0.0
。
有人能帮忙吗?谢谢:)
那里似乎有 3 个问题:
第一个:
JsonObjectRequest()
的格式似乎完全错误。正确的格式是:
JsonObjectRequest(int method,
String url,
JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener)
其次:您从地理编码 API
收到意外响应要使用 google 的地理编码 APIs,您必须首先获得 API 密钥。 格式应该类似于:(参考 this 文档)
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address&key=YOUR_API_KEY
所以你可以看到,它需要 API 键。您可以阅读 here 来获取密钥。另外 注意 纬度值和经度值之间有一个逗号 (,)。你的代码缺少那个。
第三:你得到 lat
和 lng
作为 0.0
那是因为获取位置数据需要时间。 OS 在具有一定精度的位置数据之前不会调用 onLocationChange()
。因此,当您单击该按钮时,到那时您还没有更新位置。您应该做的是,当用户单击该按钮时,向他显示一个带有消息 updating location
的进度条,并且仅在调用 onLocationChange()
时才关闭它。仅当至少调用一次 'onLocationChange()' 时才使用 lat
和 lng
。
我只需要更改 JsonObjectRequest()
的格式。根据@ZeekHuge
JsonObjectRequest(int method,
String url,
JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener)
正确的代码如下:
public class Gps3Activity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
private Button display;
private TextView displayLocation;
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);
displayLocation=(TextView)findViewById(R.id.location1);
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) {
getLocation(lat,lng);
Log.e("Latitude", String.valueOf(lat));
Log.e("Longitude", String.valueOf(lng));
Log.e("city",address);
Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG);
displayLocation.setText("City : "+address);
}
});
}
private class myLocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
//Toast.makeText(getApplicationContext(),"Latitude: "+lat+"\nLongitude: "+lng,Toast.LENGTH_LONG).show();
getLocation(lat,lng);
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
public void getLocation(double latitude,double longitude){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
"http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+longitude +"&sensor=true",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
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);
}
}
虽然使用此方法显示 location
需要花费大量时间。