How to create HashMap<String, String> through JNI then parse to java

Hussain Sherwani :

Hi I want to secure my web url and app secret keys through Ndk, I want to create hashmap in app and also store key values statically then parse to java, I follow Stackoverflow answer like Create HashMap also JNI passing objects from C++ to Java some method deprecated from above link and didn't find any way to do this, I created HashMap but it giving me error

#include <jni.h>
extern "C"
JNIEXPORT jobject
Java_com_company_project_home_ui_MainActivity_getUrlMap(
        JNIEnv *env,
        jobject /* this */) {
    jclass mapClass = env->FindClass("java/util/HashMap");
    if(mapClass == NULL)
    {
        return NULL;
    }
    jsize map_len = 1;
    jmethodID init = env->GetMethodID(mapClass, "<init>", "(I)V");
    jobject hashMap = env->NewObject(mapClass, init, map_len);
    jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
    char *key = (char*)"URL_TEST_API"  ;
    char *val = (char*)"home.php";
    env->CallObjectMethod(hashMap, put, key, val);
    return hashMap;
}

Error

A/art: art/runtime/java_vm_ext.cc:410]   native: #12 pc 00dd8f2f  /data/app/com.company.project-1/oat/arm/base.odex (void com.company.project.home.ui.MainActivity.onCreate(android.os.Bundle)+714)
Hussain Sherwani :

I solved My Problem the main Problem I found in my code is I missed to add JNICALL after JNIEXPORT line, and as Key and val are not valid so I changed to Java String through C as mentioned by @Michael in above answer, now code working fine after some modification also change ()

    #include <jni.h>
        extern "C"
        JNIEXPORT jobject
        JNICALL
        Java_com_company_project_somePackage_SplashScreen_getHashMap(JNIEnv *env, jobject /* this */) {
           jclass mapClass = env->FindClass("java/util/HashMap");
           if(mapClass == NULL)
           {
               return NULL;
           }
           jsize map_len = 1;
           jmethodID init = env->GetMethodID(mapClass, "<init>", "(I)V");
           jobject hashMap = env->NewObject(mapClass, init, map_len);
           jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
           jenv->CallObjectMethod(hashMap, put, env->NewStringUTF("URL_TEST_API"), env->NewStringUTF("home.php"));
return hashMap;
        }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to iterate through hashmap<String ArrayList<String>> and create xml file name with key String and nodes from arraylist

From Dev

JAVA - Create HashMap using string

From Dev

How To Parse String In Java With *

From Java

How to parse this string in Java?

From Dev

Java: How to parse a String?

From Java

How to parse a string which is in particular format into HashMap?

From Java

How to convert String into Hashmap in java

From Dev

How to return string array to java JNI

From Java

How to parse a string into a Java Date?

From Dev

How to parse soap string in java

From Dev

How to Parse a String with delimeters in Java

From Dev

Java: how to parse a string in this situation?

From Dev

How to parse (via loop) through a string until the end of the string?

From Java

How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

From Dev

PowerShell: Parse through xml Nodes via xpath and create output string

From Dev

How would I create and use a string to string Hashmap in Rust?

From Dev

How can I create Hashmap<String,Int : String,String : String,String> in Kotlin for sorting

From Java

Java: how to convert HashMap<String, Object> to array

From Dev

Java - How to get an Integer associated with a String in a HashMap

From Java

String to HashMap JAVA

From Dev

Convert String to Hashmap in Java

From Dev

Java Split String into Hashmap

From Dev

Escaping a Java string for JNI code

From Java

How to create a loop through LinkedHashMap<String,ArrayList<String>>?

From Java

Java8: Create HashMap with character count of a String

From Java

Create HashMap from List<String>

From Java

JNI on Android: How to retrieve a string from Java code?

From Dev

How to call from jni java library with 2 string parameters?

From Dev

How to convert the contents of a Java byte array to C string in JNI?

Related Related

  1. 1

    how to iterate through hashmap<String ArrayList<String>> and create xml file name with key String and nodes from arraylist

  2. 2

    JAVA - Create HashMap using string

  3. 3

    How To Parse String In Java With *

  4. 4

    How to parse this string in Java?

  5. 5

    Java: How to parse a String?

  6. 6

    How to parse a string which is in particular format into HashMap?

  7. 7

    How to convert String into Hashmap in java

  8. 8

    How to return string array to java JNI

  9. 9

    How to parse a string into a Java Date?

  10. 10

    How to parse soap string in java

  11. 11

    How to Parse a String with delimeters in Java

  12. 12

    Java: how to parse a string in this situation?

  13. 13

    How to parse (via loop) through a string until the end of the string?

  14. 14

    How to achieve a multi valued hashmap like HashMap<String,Hashmap<String,String>> in Java

  15. 15

    PowerShell: Parse through xml Nodes via xpath and create output string

  16. 16

    How would I create and use a string to string Hashmap in Rust?

  17. 17

    How can I create Hashmap<String,Int : String,String : String,String> in Kotlin for sorting

  18. 18

    Java: how to convert HashMap<String, Object> to array

  19. 19

    Java - How to get an Integer associated with a String in a HashMap

  20. 20

    String to HashMap JAVA

  21. 21

    Convert String to Hashmap in Java

  22. 22

    Java Split String into Hashmap

  23. 23

    Escaping a Java string for JNI code

  24. 24

    How to create a loop through LinkedHashMap<String,ArrayList<String>>?

  25. 25

    Java8: Create HashMap with character count of a String

  26. 26

    Create HashMap from List<String>

  27. 27

    JNI on Android: How to retrieve a string from Java code?

  28. 28

    How to call from jni java library with 2 string parameters?

  29. 29

    How to convert the contents of a Java byte array to C string in JNI?

HotTag

Archive