If Android Studio Button Listener fails to exceed the value, this is a question...

Asked 2 years ago, Updated 2 years ago, 78 views


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String name = search.getText().toString();

               // // Toast.makeText(SearchActivity.this, name, Toast.LENGTH_SHORT).show();

                String url = "http://000.000.000.000/search.php";

                JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                mResult = response;
                             //   Log.v("TAG", "Value of Variable"+name.toString();
                                drawList();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText (SearchActivity.this, "DB Interworking Error", Toast.LENGTH_LONG).show();
                            }
                        }) {
                    @Override
                    private Map<String,String> getParams(){
                        Map<String,String> params = new HashMap<String, String>();
                        Log.v("TAG", "Value of Variable"+name.toString();
                        params.put(KEY_TITLE, name);

                        return params;
                    }
                };

                jsObjRequest.setTag(SEARCHTAG);
                mQueue.add(jsObjRequest);
            }
        });

When I press the button above, I want to receive the value of editText as a string and pass the value of name to php using the param.put of getParams(), but I want to use a private map.Do not move on to getParams(). I used the log to check if it went over, but it didn't go over, so I'm posting this question.

android php map

2022-09-21 16:20

2 Answers

JsonObjectRequest is implemented without calling the getParams() function internally. You can only pass the Request Body through the generator's third parameter, jsonRequest. (The part you set to null in the code you uploaded) Please refer to the following sample code and correct the code.

Map<String, String> params = new HashMap<String, String>();
params.put("title", "foo");

JsonObjectRequest jsonObjRequest = new JsonObjectRequest
    (Request.Method.POST, url, new JSONObject(params), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            ...
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            ...
        }
    });


2022-09-21 16:20

If you use JsonObjectRequest, you will send the data as a Json string. Therefore, the server must also decode the Json string before importing it. Please review and apply the description and examples of the json_decode() function in the following link.

Another option is to use StringRequest on the client rather than JsonObjectRequest. If StringRequest is used, it is expected that the content-type will be sent to "application/x-www-form-urlencoded; charset=UTF-8" so it will be able to receive the value from the existing server code.

When changing StringRequest, you only need to change the generator parameter after changing JsonObjectRequest to StringRequest from the code you first uploaded. At this time, getParams() will be called, so you can use the existing code as it is.


2022-09-21 16:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.