将hashmap字符串发送到服务器php,而不是在volly android中返回JsonArray

CM =$_POST['code_send'];

$db =Db::getInstance();

$records = $db->query ("SELECT * FROM p_users WHERE c_meli='$CM'");

$js = array($records);

echo json_encode($js,JSON_UNESCAPED_UNICODE);


<!-- language: lang-java-->


//将hashmap字符串发送到服务器php,而不是在volly android中返回JsonArray

private void send_to_php2() {

   final JsonArrayRequest jsonObjectRequest = new JsonArrayRequest (Request.Method.POST,
         URL_Profile,
                new Response.Listener<JSONArray> () {
                    @Override
                    public void onResponse(JSONArray response) {
                        for (int i = 0; i < response.length (); i++) {
                            try {
                                JSONObject jsonObject = response.getJSONObject (i);
                                String name = jsonObject.getString ("l_name");
                                name_profile.setText (name);

                            } catch (JSONException e) {
                                e.printStackTrace ();
                            }

                        }
                    }
                }, new Response.ErrorListener () {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap1 = new HashMap<String, String> ();
                hashMap1.put ("code_send", cod_meli);

                return hashMap1;
            }

        };

        requestQueue.add (jsonObjectRequest);

}

<!-- end snippet -->


// end snippetend snippetend snippetend snippetend snippetend snippet
// end snippetend snippetend snippetend snippetend snippetend snippet

最佳答案

这是我的一堂课:

RequestQueue requestQueue = BaseActivity.getInstance().getRequestQueue();

    String url = UrlManager.getUrl(Urls.LIKE);

    StringRequest stringRequest = new StringRequest(
            Request.Method.POST,
            url,
            response -> {
                try {
                    JSONObject responseJSONObject = new JSONObject(response);
                    if (!responseJSONObject.isNull("status")) {
                        if (responseJSONObject.getBoolean("status"))
                            iLikeBookFinishedListener.successLikeBook(responseJSONObject.getBoolean("status"));
                        else
                            iLikeBookFinishedListener.failureLikeBook(
                                    BaseActivity
                                            .getContext()
                                            .getResources()
                                            .getString(R.string.try_later)
                            );
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            },
            error -> {
            }
    ) {
        @Override
        public byte[] getBody() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("book_id", "1");
            return new JSONObject(params).toString().getBytes();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Authenticate authenticate = Authenticate.getAuthenticateInstance();
            authenticate.loadAuthenticate();
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", authenticate.getAccessToken());
            return headers;
        }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded";
        }
    };
    RetryPolicy mRetryPolicy = new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(mRetryPolicy);
    requestQueue.add(stringRequest);


如果您需要更多说明,请询问。

10-08 16:07