Android从Web Service加载图片无输出及JSON解析错误求助
Hey there, let's fix that Volley ParseError you're hitting! The error message com.android.volley.ParseError: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray tells you exactly what's wrong: your app is using JsonArrayRequest to fetch data, but the server isn't returning a valid JSON array (something wrapped in []). Instead, it's sending back a plain string, a single JSON object, or malformed JSON.
Step 1: Verify what the server actually returns
First, open your target URL http://georeachtech.com/missing.org/fetch_home_page_data.php in a browser and check the response. You'll likely see one of these cases:
- A single JSON object (like
{"image":"your-image-url.jpg"}) - Plain text (no JSON formatting at all)
- A JSON array with extra characters (like whitespace or a prefix/suffix)
Step 2: Adjust your Volley request type
Based on what you find in Step 1, pick the right request type:
Case 1: Server returns a single JSON object
Replace JsonArrayRequest with JsonObjectRequest, since we're dealing with a single object instead of an array:
public void sendRequest() { JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, request_url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { SliderUtils sliderUtils = new SliderUtils(); try { // Update this to match the actual key in your server response sliderUtils.setSliderImageUrl(response.getString("image")); sliderImg.add(sliderUtils); } catch (JSONException e) { e.printStackTrace(); Log.e("JSON Parse", "Failed to extract image URL", e); } // Initialize ViewPager and dots as before viewPagerAdapter = new ViewPagerAdapter(sliderImg, MainActivity.this); viewPager.setAdapter(viewPagerAdapter); dotscount = viewPagerAdapter.getCount(); dots = new ImageView[dotscount]; for (int i = 0; i < dotscount; i++) { dots[i] = new ImageView(MainActivity.this); dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.nonactivate)); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(8, 0, 8, 0); sliderDotspanel.addView(dots[i], params); } // Avoid index out of bounds if no images are loaded if (dotscount > 0) { dots[0].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.activate)); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("Volley Error", error.toString()); } }); // Use your singleton RequestQueue (remove the duplicate rq.add call!) CustomVolleyRequest.getInstance(this).addToRequestQueue(jsonObjectRequest); }
Case 2: Server returns a string that needs parsing (e.g., a malformed array or plain text)
Use StringRequest to fetch the raw response, then manually parse it into a JSONArray:
public void sendRequest() { StringRequest stringRequest = new StringRequest(Request.Method.GET, request_url, new Response.Listener<String>() { @Override public void onResponse(String response) { // First clean up the response if needed (trim whitespace, remove extra characters) String cleanedResponse = response.trim(); try { JSONArray jsonArray = new JSONArray(cleanedResponse); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); SliderUtils sliderUtils = new SliderUtils(); sliderUtils.setSliderImageUrl(jsonObject.getString("image")); sliderImg.add(sliderUtils); } // Initialize ViewPager and dots here (same as the previous example) viewPagerAdapter = new ViewPagerAdapter(sliderImg, MainActivity.this); viewPager.setAdapter(viewPagerAdapter); // ... rest of the dot initialization code } catch (JSONException e) { e.printStackTrace(); Log.e("Parse Error", "Failed to parse response: " + cleanedResponse, e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e("Volley Error", error.toString()); } }); CustomVolleyRequest.getInstance(this).addToRequestQueue(stringRequest); }
Bonus: Fix your RequestQueue initialization bug
In your onCreate() method, you're initializing the RequestQueue twice:
rq = CustomVolleyRequest.getInstance(this).getRequestQueue(); rq = Volley.newRequestQueue(this); // This overrides the singleton queue!
Remove the second line (Volley.newRequestQueue(this)) to use your singleton CustomVolleyRequest properly — this ensures you're reusing the same request queue instance throughout your app.
Final check
Make sure the image field returned by the server is a full, valid URL (starts with http:// or https://). If it's a relative path (like /images/photo.jpg), you'll need to prepend the base URL of your server to it before passing it to Volley's ImageLoader.
内容的提问来源于stack exchange,提问作者Tabish Shaikh

