我是Android新手。我只想按用户ID从我的数据库中获取广告,但会收到类似org.json.JSONException: End of input at character 0 of
的错误。无需发布用户ID并仅获取数据库中的所有广告即可。
private void loadProducts(final String user_id) {
StringRequest stringRequest = new StringRequest(Request.Method.GET,
AppConfig.URL_USER_ADVERTISEMENT_LIST,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// Converting the string to json array object.
Log.i("tagconvertstr", "["+response+"]");
JSONArray array = new JSONArray(response);
// Traversing through all the object.
for (int i = 0; i < array.length(); i++) {
// Getting advertisement object from json array.
JSONObject advertisement =
array.getJSONObject(i);
// Adding the user advertisement to user adv list
UserAdvertisementList.add(
new UserAdvertisementListObject(
advertisement.getString("uid"),
advertisement.getString("name"),
advertisement.getString("datetime"),
advertisement.getString("from_city"),
advertisement.getString("to_city"),
advertisement.getString("car_name"),
advertisement.getInt("car_make_year"),
advertisement.getInt("phone"),
advertisement.getInt("number_of_places"),
advertisement.getDouble("price")
));
}
// Creating adapter object and setting it to recycler
// view
UserAdvertisementListAdapter adapter = new
UserAdvertisementListAdapter(
UserAdvertisementListActivity.this.getActivity(),
UserAdvertisementList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Advertisement List Error: " +
error.getMessage());
Toast.makeText(getActivity().getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to advertisement list url.
Map<String, String> params = new HashMap<String, String>();
params.put("user_id", user_id);
return params;
}
};
// Adding our string request to queue.
Volley.newRequestQueue(
UserAdvertisementListActivity.this.getActivity()).add(stringRequest);
}
这是我的php文件,我想在其中获取user_id
,然后通过此user_id
获取所有数据:
if (isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$stmt = $conn->prepare("SELECT unique_id, name, phone, from_city, to_city, car_name, car_make_year, number_of_places, price, datetime
FROM users
INNER JOIN advertisement ON users.unique_id = advertisement.user_id
WHERE users.unique_id = ?");
$stmt->bind_param("s", $user_id);
$stmt->execute();
$stmt->bind_result($user_id, $name, $phone, $from_city, $to_city, $car_name, $car_make_year, $number_of_places, $price, $datetime);
$products = array();
while($stmt->fetch())
{
$temp = array();
$temp['uid'] = $user_id;
$temp['name'] = $name;
$temp['phone'] = $phone;
$temp['from_city'] = $from_city;
$temp['to_city'] = $to_city;
$temp['car_name'] = $car_name;
$temp['car_make_year'] = $car_make_year;
$temp['number_of_places'] = $number_of_places;
$temp['price'] = $price;
$temp['datetime'] = $datetime;
array_push($products, $temp);
}
echo json_encode($products);
}
错误:
W/System.err: org.json.JSONException: Value {"error":false} of type
org.json.JSONObject cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
at com.vladimirspucko.organizer.activity.UserAdvertisementListActivity$1.onResponse(UserAdvertisementListActivity.java:108)
at com.vladimirspucko.organizer.activity.UserAdvertisementListActivity$1.onResponse(UserAdvertisementListActivity.java:102)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
答案 0 :(得分:0)
将Request.Method.GET
更改为Request.Method.POST
,并解决了我的问题。