Android Studio中Java代码JsonArrayRequest构造器匹配错误修复求助
Hey there, let's work through this Volley issue step by step — I've been in your shoes as a new Android/Java developer, so I know how frustrating constructor mismatches can be!
Why You're Seeing This Error
The "JSON Array request cannot be applied" error almost always boils down to mismatched constructor parameters or missing setup steps. Common culprits include:
- Using an outdated Volley version that doesn't support the constructor you're calling
- Importing the wrong
JsonArrayRequestclass (not the official Volley one) - Forgetting to initialize core components like
RequestQueue - Using the wrong request type for your server's expected input (e.g., sending JSON when the server expects form data)
Step-by-Step Fix
1. Verify Volley Dependency
First, make sure you're using the latest stable Volley version in your module-level build.gradle file:
dependencies { implementation 'com.android.volley:volley:1.2.1' }
Sync your project after updating this.
2. Check Import Statements
Ensure you're importing the correct Volley classes at the top of your Activity/Fragment:
import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonArrayRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
3. Initialize RequestQueue
You can't send requests without initializing RequestQueue first. Add this to your onCreate method:
private RequestQueue requestQueue; private List<OurData> mList = new ArrayList<>(); private YourAdapter mAdapter; // Replace with your actual Adapter class name @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); // Initialize RequestQueue requestQueue = Volley.newRequestQueue(this); // Initialize your list and adapter (adjust to your setup) mAdapter = new YourAdapter(this, mList); // Attach adapter to your RecyclerView/ListView here }
4. Fixed request() Method
Here's your updated method with fixes and improvements:
private void request() { Log.d("VOLLE------","MAUKKKK"); String url = "YOUR_API_ENDPOINT_URL"; // Replace with your actual API URL // Use JsonArrayRequest for POST requests with JSON body JsonArrayRequest requestItem = new JsonArrayRequest( Request.Method.POST, url, null, // Pass a JSONObject here if your server expects JSON payload (e.g., new JSONObject("{\"key\":\"value\"}")) new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { mList.clear(); // Clear old data to avoid duplicates for (int i = 0; i < response.length(); i++) { try { JSONObject data = response.getJSONObject(i); OurData item = new OurData(); item.setId(data.getString("idperawatan")); item.setmText1(data.getString("nama")); item.setmText2(data.getString("alamat")); item.setmImageResource(data.getString("image")); mList.add(item); } catch (JSONException e) { Log.e("VOLLEY_ERROR", "JSON Parsing Failed: " + e.getMessage()); e.printStackTrace(); } } // Update UI safely (lambda simplifies the Runnable) runOnUiThread(() -> mAdapter.notifyDataSetChanged()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("VOLLEY_ERROR", "Request Failed: " + (error.getMessage() != null ? error.getMessage() : "Unknown Error")); // Optional: Show user feedback // Toast.makeText(getApplicationContext(), "Request failed, try again later", Toast.LENGTH_SHORT).show(); } } ); // Optional: Add a tag to cancel the request later (e.g., onDestroy) requestItem.setTag("POST_DATA_REQUEST"); requestQueue.add(requestItem); }
5. If Your Server Expects Form Data (Not JSON)
If your API uses x-www-form-urlencoded parameters instead of a JSON body, use StringRequest instead of JsonArrayRequest:
private void request() { Log.d("VOLLE------","MAUKKKK"); String url = "YOUR_API_ENDPOINT_URL"; StringRequest stringRequest = new StringRequest( Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONArray jsonArray = new JSONArray(response); mList.clear(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject data = jsonArray.getJSONObject(i); OurData item = new OurData(); item.setId(data.getString("idperawatan")); item.setmText1(data.getString("nama")); item.setmText2(data.getString("alamat")); item.setmImageResource(data.getString("image")); mList.add(item); } runOnUiThread(() -> mAdapter.notifyDataSetChanged()); } catch (JSONException e) { Log.e("VOLLEY_ERROR", "JSON Parsing Failed: " + e.getMessage()); e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("VOLLEY_ERROR", "Request Failed: " + (error.getMessage() != null ? error.getMessage() : "Unknown Error")); } } ) { @Override protected Map<String, String> getParams() throws AuthFailureError { // Add your form parameters here Map<String, String> params = new HashMap<>(); // params.put("username", "test_user"); // Example parameter return params; } }; stringRequest.setTag("POST_FORM_REQUEST"); requestQueue.add(stringRequest); }
Final Checks
- Make sure your
OurDataclass has all the setter methods you're calling (setId,setmText1, etc.) - Ensure your API endpoint is reachable and returns a valid JSON array
- Cancel pending requests in
onDestroyto avoid memory leaks:@Override protected void onDestroy() { super.onDestroy(); if (requestQueue != null) { requestQueue.cancelAll("POST_DATA_REQUEST"); } }
内容的提问来源于stack exchange,提问作者Veyonathan Nico

