2016-10-26 2 views
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입니다. 뭐가 문제 야?

+0

매개 변수가없는 메소드가 확실합니까? 나는 그것이 'onCountryListReceived (Object result)'또는 무언가 – mihail

+0

을 지적해야한다고 가정합니다. 매개 변수가 전달되지 않았습니다. –

+0

답장을 게시하면 답을 게시하여 다른 사람에게 도움이 될 수 있습니다. :) – mihail

답변

0

Object.class 대신 getDeclaredMethod의 JsonObject.class가 호출되는 메서드의 매개 변수이므로 전달했습니다.
getDeclaredMethod는 두 개의 매개 변수를 허용합니다.
1. 콜백 메소드 이름 2. 해당 메소드의 매개 변수.

관련 문제