JSONException :在字符 0 处的输入结束,响应返回 0



我正在尝试使用JSONArray所以我按照一些教程进行编辑,这是我的代码。

private void showData(final String sid) {
    RequestQueue requestQueue = Volley.newRequestQueue(mContext);
    JsonArrayRequest jObjReq = new JsonArrayRequest(
            PORT_URL, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            final ArrayList<Inventory> inventory = new ArrayList<>();
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jObj = response.getJSONObject(i);
                    String date = jObj.getString("date");
                    String code = jObj.getString("codesec");
                    String name = jObj.getString("secname");
                    String vol = jObj.getString("volume");
                    Inventory in = new Inventory();
                    in.setDate(date);
                    in.setCode(code);
                    in.setName(name);
                    in.setVol(vol);
                } catch (JSONException e) {
                    e.printStackTrace();
                    SimpleTableDataAdapter adapter = new SimpleTableDataAdapter(c, new TableHelper(c).DataArray(inventory));
                    tableView.setDataAdapter(adapter);
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("sid", sid);
            return params;
        }
    };
    requestQueue.add(jObjReq);
}

以及我想显示的输出(假设已经输入了 SID)

[
  {
    "0": "8",
    "1": "1305199706",
    "2": "2017-10-10",
    "3": "SME",
    "4": "Super Mechanic Electro",
    "5": "500",
    "id": "8",
    "sid": "1305199706",
    "date": "2017-10-10",
    "codesec": "SME",
    "secname": "Super Mechanic Electro",
    "volume": "500"
  },
  {
    "0": "9",
    "1": "1305199706",
    "2": "2017-10-11",
    "3": "RRXE",
    "4": "Recontruction Rex Expert",
    "5": "10,000",
    "id": "9",
    "sid": "1305199706",
    "date": "2017-10-11",
    "codesec": "RRXE",
    "secname": "Recontruction Rex Expert",
    "volume": "10,000"
  }
]

轮到它error org.json.JSONException : End of input at character 0在日志猫上。 我不知道我错过了什么...

您可以通过 Log 检查从服务器接收的数据长度。使用 Log.d("TAG","Length :- "+response.length()); 检查数组的长度如果长度为 0,则检查您正在调用的 url。希望对您有所帮助。

如果您使用的是本地服务器并从模拟器进行调试。将 localhost 替换为 URL 中的10.0.2.2

如果您使用的是在同一网络中连接的电话,请将localhost替换为 URL 中本地服务器的IP Address

此错误是因为您的 API 未向调用函数返回任何值/JSON

检查您的网址是否正常工作,以及该网址返回值

最新更新