简体   繁体   中英

Android:Method Callback is not working

I am trying to implement method callback using relection but it is not calling the desired method.Here is my main activity

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RetrofitCalls retrofitCalls = new RetrofitCalls();
        retrofitCalls.requestAccesstoken();
        retrofitCalls.requestDataFromServer(MainActivity.this,"onCountryListReceived");
    }

    public void onCountryListReceived(JsonObject jsonObject)
    {
        String temp = "hello";
    }
}

and here is the requestDataServer method

 public void requestDataFromServer(final Activity activity, final String callBackMethod){

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new HeaderInterceptor())
            .build();

    retrofit = new Retrofit.Builder()
            .baseUrl(Constants.ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();

    Api api = retrofit.create(Api.class);
    Call<JsonObject> call = api.getDataFromServer(Constants.COUNTRY_LIST);
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {

            if(response.errorBody() != null){
                try {
                    String j1 = response.errorBody().string();
                    String temp = "hello";
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }else if(response.body() != null) {
                JsonObject jsonObject = response.body();
                JsonArray jsonArray = jsonObject.getAsJsonArray("Data");
                String temp = "hello";

                try {
                    String x = activity.getClass().toString();
                    Method method = activity.getClass().getDeclaredMethod(callBackMethod.trim(),Object.class);
                    method.invoke(activity,jsonObject);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            String temp = "hello";
        }
    });
}

why the onCountryListReceived is not invoked? It is giving NoSuchMethodException . What's the problem?

Instead of Object.class i passed JsonObject.class in getDeclaredMethod as it is the parameter of the method to be called.
getDeclaredMethod accepts two parameters
1.callback method name 2.parameters of that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM