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()
{
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);
} 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?
Aucun commentaire:
Enregistrer un commentaire