Search code examples
androidjsonstringint

Error:(54, 91) error: incompatible types: int cannot be converted to String


I'm trying to connect my app with server but getting an error. Please help!

I'm trying to connect my app using volley library to the server,but whenever I try to run my code I get the following error

incompatible types: int cannot be converted to String

I tried changing from String to int, but it didn't help!

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

                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,          **<-------------error here**
                            showUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray student = response.getJSONArray("student");
                                for (int i=0;i<student.length();i++){
                                    JSONObject students = student.getJSONObject(i);

                                    String firstname = students.getString("firstname");
                                    String lastname = students.getString("lastname");
                                    String age = students.getString("age");

                                    result.append(firstname+""+lastname+""+age+""+"\n");
                                }
                                result.append("==\n");

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

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

                        }
                    });
                    requestQueue.add(jsonObjectRequest);
                }
            });
        }
    }

Solution

  • Got your problem now.

    You're using incorrect method of JsonObjectRequest from Volley.

    You're calling this method

    JsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)

    instead of

    JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)

    where first argument is an int.

    You're passing in 4 arguments, so the upper method is being called where your int method is being converted to String url

    You can fix it by passing 5 arguments, and all should be good then.