Android凌空没有发送params

时间:2017-11-11 04:05:51

标签: android android-volley

以下不起作用的代码。看到这段代码有什么问题,为什么它不发送参数。我是Android新手。

  private void sendParams()
{
    JsonArrayRequest movieReq = new JsonArrayRequest(AppConfig.URL_Q_RECIPIES,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Recipie movie = new Recipie();
                            movie.setRecipieName(obj.getString("recipie_name"));
                            movie.setId(obj.getInt("id"));
                            // build the image URL here
                            String s =   AppConfig.URL_IMAGE + obj.getInt("id") + ".jpeg";
                            movie.setImageURL(s);
                            movie.setPrimaryIngrediant(obj.getString("prim_ingr"));
                            movie.setUrl(obj.getString("url"));


                            // adding movie to movies array movie is nothing but recipie
                            movieList.add(movie);

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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Log.e(TAG, "Filter error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hidePDialog();



        }
    }) {

            @Override
            protected Map<String, String> getParams () {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("ind_ajwain", Integer.toString(session.isAjwainChecked()));
                //params.put("ind_asaftide", Integer.toString(session.isAsaftideChecked()));
                params.put("rec_name", "chicken");  /// change this
                params.put("ind_ginger", "0");



                //params.put("password", password);

                return params;
            }

    };
    Log.d(TAG, movieReq.toString());
    //movieReq.

    AppController.getInstance().addToRequestQueue(movieReq);
}

我按照此链接开始编码:来源:https://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

我上面没有做太多改动,只更改参数。

2 个答案:

答案 0 :(得分:1)

使用Volley将数据发送到服务器请参考下面的代码。

第1步:使用POST方法发送数据。

创建以下方法:

private void makeJsonSend() {

    StringRequest jsonObjReq = new StringRequest(Request.Method.POST,Const.ServiceType.URL,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.e("response:forgot", response);
                    jsonParseResponse(response);
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {


            //show error message here
        }
    }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("email","email");

            //pass parameter here

            Log.i("request send data", params.toString());

            return params;
        }


        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();

            //If you have header parameter then set here
            return headers;
        }


    };

   AppController.getInstance().addToRequestQueue(movieReq);

}

第2步:您已获得该操作响应的响应方法。

private void jsonParseResponse(String response) {

    try {
        JSONObject jsonObject = new JSONObject(response);

        if (jsonObject.getString("status").equals("200")) {

            cc.showToast(jsonObject.getString("message"));




        } else {

            cc.showToast(jsonObject.getString("message"));
        }

    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("Forgot Error",e.toString());
    }

}

答案 1 :(得分:0)

您未在new JsonArrayRequest中传递任何请求方法。您必须通过Request.Method.POSTRequest.Method.GET