简体   繁体   中英

Android volley is not sending params

The code which does not work is below. See anything wrong with this code , why it is not sending parameters. I am new to 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);
}

I followed this link to get coding don : Source: https://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

I have not made much changes above, only changing parameters.

For sending data to server using Volley refer below code.

Step1 : Use POST method to send data.

Create below method :

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);

}

Step 2: Response method where you have get response of that operation.

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());
    }

}

You didn't pass any request method in new JsonArrayRequest . You have to pass Request.Method.POST or Request.Method.GET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM