Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

android - Return a String array on a JNI method

I need to get a List of Strings (char*) from C++ and return it to Java.

How can I do that?

I think one solution is return a big string pre-defined like: "[item1][item2]" and make a split on Java, but it doesn't look like the right approach.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Look at NewObjectArray into the JNI doc.

Basically you can return from the JNI function an Array Of String (Java) an then transform it in a List or a whatever kind of Collection type.

Peudo code:

Java:

....
public List<String> getFooAsList(){
  return new ArrayList(this.getData());
}  
private native String[] getData();

JNI

#include <jni.h>


  JNIEXPORT jobjectArray JNICALL 
               como_foo_bar_getData
  (JNIEnv *env, jobject jobj){

    jobjectArray ret;
    int i;

    char *data[5]= {"A", "B", "C", "D", "E"};

    ret= (jobjectArray)env->NewObjectArray(5,env->FindClass("java/lang/String"),env->NewStringUTF(""));

    for(i=0;i<5;i++) env->SetObjectArrayElement(ret,i,env->NewStringUTF(data[i]));

    return(ret);
 }

Not tested!!!

Let me know if it works for u

Regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...