Android应用调用API出现400错误求助(Volley调用USGS地质数据接口)
Hey there! Let's break down why you're hitting that 400 error—this almost always means the server can't parse your request correctly. Let's walk through the most common fixes step by step:
1. Double-Check Required API Parameters
The USGS MRDS bounding box search API has mandatory parameters you might be missing or formatting incorrectly:
bbox: Must be four comma-separated coordinates (minx, miny, maxx, maxy) with no spaces (e.g.,-120,35,-118,37). This defines the geographic area you're querying.format(recommended): Specify a response format likejsonorxmlto ensure the server returns data your app can handle. Without this, the default response might cause parsing issues.
If any required parameter is missing or malformed, the server will throw a 400 error immediately.
2. Validate Your Request Method & URL Construction
This API uses GET requests—so make sure you're not accidentally sending a POST request with Volley. For GET requests, parameters need to be appended to the URL (not sent in the request body).
Use Uri.Builder to safely build your URL (it handles automatic URL encoding):
String baseUrl = "https://mrdata.usgs.gov/mrds/search-bbox.php"; Uri.Builder urlBuilder = Uri.parse(baseUrl).buildUpon(); urlBuilder.appendQueryParameter("bbox", "-120.0,35.0,-118.0,37.0"); // Replace with valid coords urlBuilder.appendQueryParameter("format", "json"); String finalUrl = urlBuilder.build().toString();
3. Test the URL Directly Outside Volley
Copy the finalUrl from your logs and paste it into a browser or tool like Postman. If you still get a 400 error here, the problem is with your parameters/URL—not Volley. If the browser returns valid data, then the issue is in your Volley request setup.
4. Add a User-Agent Header (Sometimes Required)
Some servers block requests without a User-Agent header. Add this to your Volley request to avoid being flagged:
StringRequest request = new StringRequest(Request.Method.GET, finalUrl, response -> Log.d("Rest Response", "Success: " + response), error -> Log.e("Rest Response", error.toString())) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); headers.put("User-Agent", "Android Volley Client"); return headers; } };
5. Full Working Example
Here's a complete Volley request snippet that should work if you use valid coordinates:
// Initialize Volley request queue RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); // Build valid request URL String baseUrl = "https://mrdata.usgs.gov/mrds/search-bbox.php"; Uri.Builder urlBuilder = Uri.parse(baseUrl).buildUpon(); urlBuilder.appendQueryParameter("bbox", "-120.0,35.0,-118.0,37.0"); urlBuilder.appendQueryParameter("format", "json"); String finalUrl = urlBuilder.build().toString(); // Create and enqueue request StringRequest stringRequest = new StringRequest(Request.Method.GET, finalUrl, response -> { // Handle successful response here Log.d("Rest Response", "Received data: " + response.substring(0, 100) + "..."); }, error -> { // Handle error Log.e("Rest Response", error.toString()); }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> headers = new HashMap<>(); headers.put("User-Agent", "Android Volley Example"); return headers; } }; queue.add(stringRequest);
Start by verifying your bbox coordinates and testing the URL in a browser—this will almost certainly point you to the root cause.
内容的提问来源于stack exchange,提问作者user2161312

