在改造回调实现中遇到错误

时间:2019-03-11 07:30:33

标签: android json retrofit

我已经编写了一个用于检索数据的改装回调,如下所示

 AppConfig.read api = adapter.create(AppConfig.read.class);
    api.readData(new Callback() {
        @Override
        public void success(JsonElement result, Response response) {

            String myResponse = result.toString();
            Log.d("response", "" + myResponse);
            try {
                JSONObject jObj = new JSONObject(myResponse);
                int success = jObj.getInt("success");
                if (success == 1) {
                    JSONArray jsonArray = jObj.getJSONArray("details");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jo = jsonArray.getJSONObject(i);
                        id.add(jo.getString("id"));
                        name.add(jo.getString("name"));
                        age.add(jo.getString("age"));
                        mobile.add(jo.getString("mobile"));
                        email.add(jo.getString("email"));
                        details_list.setAdapter(displayAdapter);
                    }
                } 
                else {
                    Toast.makeText(getApplicationContext(), "No Details 
                    Found", Toast.LENGTH_SHORT).show();
                }
                } catch (JSONException e) {
                      Log.d("exception", e.toString());
            }
        }
            @Override
            public void failure(RetrofitError error) {
                Log.d("Failure", error.toString());
                Toast.makeText(StudReg.this, error.toString(), Toast.LENGTH_LONG).show();
            }

    }
  );
 }

我的API接口如下

   public interface read {
    @GET("/displayAll.php")
    void readData(Callback<JsonElement> callback);
}

但是我收到一个错误,Class 'anonymous class derived from callback' must either be declared abstract or implement abstract method 'success(T,Response)' in CallBack

请帮助我。非常感谢

1 个答案:

答案 0 :(得分:0)

在设置通用类时,请更改以下代码。
更改此声明声明
api.readData(new Callback())

api.readData(new Callback<JsonElement>()) <br/>

如您在接口中所定义的那样 readData方法将采用通用类的参数

Callback<JsonElement>

我可以在这里的接口声明中找到

void readData(Callback<JsonElement> callback);

这就是编译器似乎抱怨的原因