vendredi 26 octobre 2018

Java Reflection - Wrong number of arguments; expected 0, got 1

I am running this java code

ConnectivityManager connectivityManager = ((ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE));
    try {
        Method method = connectivityManager.getClass().getDeclaredMethod("getTetherableIfaces");
        String[] strings = ((String[]) method.invoke(connectivityManager));
        Log.i("hotspot", "getIface: "+strings.toString());
        Method methodTether = connectivityManager.getClass().getDeclaredMethod("tether",String.class);
        methodTether.setAccessible(true);
        String[] param =new String[]{"wlan0"};

        int i = (int) method.invoke(connectivityManager,"wlan0");
        Log.i(TAG, "getIface: "+ "errorcode"+ i);

    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

but i get this error

 java.lang.IllegalArgumentException: Wrong number of arguments; expected 0, got 1
    at java.lang.reflect.Method.invoke(Native Method)

And this is the tether function which i am trying to invoke.

 public int tether(String iface) {
    try {
        return mService.tether(iface);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}

I tried to invoke the method with object[]{"wlan0"}, String[]{"wlan0"}, (object){"wlan0"}, {"wlan0"} and (Object[])String[]{"wlan0"} but i get the same error. I am not able to understand what am i doing wrong. For Any help I would be thankful.





Aucun commentaire:

Enregistrer un commentaire