i am trying to retrieve data with volley but i keep getting this error:
This is my json file which is accessed with this link "http:/192.168.1.4/pim/get_all_bus.php" where 192.168.1.4 is my local ip address
{
"Bus": [
{
"id_bus": "3",
"name_bus": "51A",
"terminusDepart": "Ennasr1",
"terminus_arrivee": "TGM"
},
{
"id_bus": "4",
"name_bus": "51A",
"terminusDepart": "Ennasr2",
"terminus_arrivee": "tunis"
}
],
"success": 1
}
the error that i am getting is this
04-09 14:40:00.589 5256-5256/info.androidhive.volleyexamples E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.androidhive.volleyexamples, PID: 5256
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.android.volley.Request.<init>(Request.java:136)
at com.android.volley.toolbox.JsonRequest.<init>(JsonRequest.java:58)
at com.android.volley.toolbox.JsonArrayRequest.<init>(JsonArrayRequest.java:42)
at info.androidhive.volleyexamples.JsonRequestActivity.makeJsonArryReq(JsonRequestActivity.java:111)
at info.androidhive.volleyexamples.JsonRequestActivity.onClick(JsonRequestActivity.java:146)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
the function that i am using is this one :
private void makeJsonArryReq() {
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest("http:/192.168.1.4/pim/get_all_bus.php",
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
//msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req,
tag_json_arry);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
}
Here you need to call JsonObjectRequest first. Your given link, returns a json object. After getting the json object, you need to parse the data.
Quick tip. In the JSON format curly brackets "{}" defines an object and third brackets "[]" defines an array.
Now look at the response from your PHP script. It starts with a curly bracket. That means your response is a JSON object. Therefore, you need to call a JSONObjectRequest from volley and not a JSONArrayRequest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.