错误:(54,91)错误:不兼容的类型:int无法转换为String

问题描述 投票:0回答:1

我正在尝试将我的应用程序与服务器连接但收到错误。请帮忙!

我正在尝试使用volley库将我的应用程序连接到服务器,但每当我尝试运行我的代码时,我都会收到以下错误

不兼容的类型:int无法转换为String

我尝试从String更改为int,但它没有帮助!

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);
                }
            });
        }
    }
android json string int
1个回答
1
投票

现在有问题了。

你使用JsonObjectRequestVolley方法不正确。

你正在调用这种方法

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

代替

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

第一个参数是int

你传递了4个参数,所以上面的方法被调用,你的int method被转换为String url

你可以通过传递5个参数来修复它,然后一切都应该是好的。

© www.soinside.com 2019 - 2024. All rights reserved.