我在通过 volley 解析 Json 时遇到问题,这是我的 logcat
08-13 06:05:55.523: D/Volley(902): [134] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://www.petbasket.in/service/getProduct.php?category_id=Dog%20Soft%20Toys 0xe4f6ef05 NORMAL 1> [lifetime=12938], [size=158], [rc=200], [retryCount=0]
08-13 06:05:55.563: W/System.err(902): com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
08-13 06:05:55.563: W/System.err(902): at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:73)
08-13 06:05:55.563: W/System.err(902): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:116)
08-13 06:05:55.573: W/System.err(902): Caused by: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
08-13 06:05:55.573: W/System.err(902): at org.json.JSON.typeMismatch(JSON.java:111)
08-13 06:05:55.573: W/System.err(902): at org.json.JSONObject.<init>(JSONObject.java:158)
08-13 06:05:55.573: W/System.err(902): at org.json.JSONObject.<init>(JSONObject.java:171)
08-13 06:05:55.573: W/System.err(902): at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:68)
我用于解析的代码是
我正在使用此代码来解析 NAYOSO
RequestQueue reqQueue = Volley.newRequestQueue(this);
Log.d(TAG, "just after line one in volley");
Log.d(TAG, url.toString());
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,
url.toString(), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
parseJSONResponse(response);
Log.d(TAG, "chal hat be" + response);
listViewAdapterAfterSelection.notifyDataSetChanged();
progress.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Network Problem",
Toast.LENGTH_LONG).show();
error.getCause();
error.printStackTrace();
progress.dismiss();
}
});
jr.setRetryPolicy(new DefaultRetryPolicy(20000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
reqQueue.add(jr);
progress = ProgressDialog.show(this, "Please Wait...",
"Loading Products from Server...");
/* ================================================================ */
}
private void parseJSONResponse(JSONObject response) {
try {
Log.v(TAG, "Before array");
JSONArray entries = response.getJSONArray("products");
for (int count = 0; count < entries.length(); count++) {
JSONObject anEntry = entries.getJSONObject(count);
Product product = new Product(anEntry.optInt("entity_id"),
anEntry.optString("name"),
anEntry.optString("short_description"),
anEntry.optInt("price"), anEntry.optString("thumbnail"));
afterSelectionItems.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
}`
回应是:
08-13 08:13:50.213: D/Volley(1723): [156] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://www.petbasket.in/service/getProduct.php?category_id=Utility 0xe4f6ef05 NORMAL 1> [lifetime=9563], [size=159], [rc=200], [retryCount=0]