0
작동하지 않는 나의 주요 활동안드로이드 : 방법 콜백 내가 relection를 사용하는 방법 콜백을 구현하기 위해 노력하고 있지만 원하는 method.Here를 호출하지 않는
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";
}
}
이며, 여기에 requestDataServer
방법
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";
}
});
}
입니다
onCountryListReceived
이 호출되지 않는 이유는 무엇입니까? 그것은 NoSuchMethodException
입니다. 뭐가 문제 야?
매개 변수가없는 메소드가 확실합니까? 나는 그것이 'onCountryListReceived (Object result)'또는 무언가 – mihail
을 지적해야한다고 가정합니다. 매개 변수가 전달되지 않았습니다. –
답장을 게시하면 답을 게시하여 다른 사람에게 도움이 될 수 있습니다. :) – mihail