我是一名正在编程的学生,正在构建一个使用api json url的Android应用,并且找不到适合JsonArrayRequest的构造函数

这是错误:


C:\ Users \ jerma \ AndroidStudioProjects \ VolleyParsing \ app \ src \ main \ java \ com \ jermainebjonesgmail \ volleyparsing \ MainActivity.java:37:
错误:找不到适合的构造函数
JsonArrayRequest(int,String,>,)
JsonArrayRequest arrayRequest =新的JsonArrayRequest(Method.GET,
^
构造函数JsonArrayRequest.JsonArrayRequest(String,Listener,ErrorListener)
不适用
(实际和正式论点列表的长度不同)
构造函数JsonArrayRequest.JsonArrayRequest(int,String,JSONArray,Listener,ErrorListener)
不适用


这是我的代码:

public class MainActivity extends AppCompatActivity {

private final static String URL = "https://age-of-empires-2-api.herokuapp.com/api/v1/units";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RequestQueue queue = Volley.newRequestQueue(this);

    JsonArrayRequest arrayRequest = new JsonArrayRequest(Method.GET,
            URL, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {

            Log.d("Response: ", response.toString());

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

            VolleyLog.d("Error", error.getMessage());
        }
    });

    queue.add(arrayRequest);


  }
}

最佳答案

您以错误的方式创建了JsonArrayRequest。有关构造函数,请参见java docs

构造器摘要

JsonArrayRequest(String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener)
      Creates a new request.


JsonArrayRequest可以如下创建:

Request request = new JsonArrayRequest(httpMethod, url, params, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            serverCallback.onAPIResponse(apiTag, response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            serverCallback.onErrorResponse(apiTag, error);
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return headers != null ? headers : super.getHeaders();
        }
    };


请找到示例代码here

08-17 22:29