我正在尝试使用凌空检索数据,但我一直收到此错误:
这是我的json文件,使用此链接访问“http:/192.168.1.4/pim/get_all_bus.php”,其中192.168.1.4是我的本地IP地址
{
"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
}
我得到的错误就是这个
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)
我正在使用的功能就是这个:
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);
}
答案 0 :(得分:0)
这里你需要先调用JsonObjectRequest。您给定的链接,返回一个json对象。获取json对象后,需要解析数据。
答案 1 :(得分:0)
快速提示。在JSON格式中,大括号“{}”定义了一个对象,第三个括号“[]”定义了一个数组。
现在查看PHP脚本的响应。它以大括号开头。这意味着您的响应是一个JSON对象。因此,您需要从齐射调用JSONObjectRequest而不是JSONArrayRequest。