来自网址的回复是空的 - Android

时间:2017-10-06 10:55:42

标签: java android android-volley

我有一个问题,当我想从url获取来自Android监视器的json数据时会出现此错误:(JSONException:字符0处的输入结束),我知道它的意思是响应不为空但是为空。但我怎么能解决它?!这里我用id的url: http://192.168.56.1/App/msg.php?id=(hisID)

并且用户总是通过输入id从设备进入。

代码:

private void ShowMessages(final String id) {
    // Tag used to cancel the request

    String tag_string_req = "req";

    progressBar.setVisibility(View.VISIBLE);

    String URL_MESSAGE = AppConfig.DNIANAS_URL + "msg.php?id=" + ID;

    final StringRequest strReq = new StringRequest(Request.Method.DEPRECATED_GET_OR_POST,
            URL_MESSAGE, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Response: " + response);
            progressBar.setVisibility(View.GONE);

            try {

                JSONObject jsonObject = new JSONObject(response);

                final JSONArray jsonArray = jsonObject.getJSONArray("call");

                for (int c = 0; c < jsonArray.length(); c++) {

                    JSONObject u = jsonArray.getJSONObject(c);
                    String m = u.getString("m");
                    String sender = u.getString("sender");

                    HashMap<String, String> contact = new HashMap<>();

                    contact.put("m", m);
                    contact.put("sender",sender);

                    messages.add(contact);

                    ListAdapter adapter = new SimpleAdapter(ChatActivity.this, messages,
                            R.layout.list_item_message, new String[]{"m","sender"},
                            new int[]{R.id.msg_user});
                    lv.setAdapter(adapter);

                }

            }
            catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            NetworkResponse networkResponse = error.networkResponse;
            Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
            Toast.makeText(getApplicationContext(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }

    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("id", id);
            return params;
        }

    };

    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

0 个答案:

没有答案