I try to create REST client and i need to use my custom method. I find answer how to do it, but when I run this code in Android srudio, I have
java.lang.NoSuchFieldException: No field methods in class Ljava/net/HttpURLConnection; (declaration of 'java.net.HttpURLConnection' appears in /system/framework/core-libart.jar)
Same code in IntelliJ IDEA works, its create warnings but work!
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by main (file:/D:/java/untitled1/out/production/untitled1/) to field java.lang.reflect.Field.modifiers WARNING: Please consider reporting this to the maintainers of main WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
private static void allowMethods(String... methods) {
try {
Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
methodsField.setAccessible(true);
String[] oldMethods = (String[]) methodsField.get(null);
Set<String> methodsSet = new LinkedHashSet<>(Arrays.asList(oldMethods));
methodsSet.addAll(Arrays.asList(methods));
String[] newMethods = methodsSet.toArray(new String[0]);
methodsField.set(null/*static field*/, newMethods);
} catch (NoSuchFieldException e) {
throw new IllegalStateException(e);
}catch (IllegalAccessException e){
e.printStackTrace();
}
}
In my main class I call allowMethods("MOVE") and in new HttpURLConnection use con.setRequestMethod("MOVE");
How I can to fix this problem? Thank you.
Aucun commentaire:
Enregistrer un commentaire