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

时间:2017-10-26 12:50:35

标签: android arrays json

我尝试使用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"
  }
]

在logcat上转向error org.json.JSONException : End of input at character 0。我不知道我想念的是什么...

2 个答案:

答案 0 :(得分:0)

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

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

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

检查您的网址是否正常,该网址返回值

答案 1 :(得分:0)

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