Rust 尝试在JNI中创建并向Arraylist添加字符串

Rust 尝试在JNI中创建并向Arraylist添加字符串,rust,java-native-interface,Rust,Java Native Interface,我有一个未知大小的rust向量,它只填充了字符串,我试图用JNI板条箱将其作为Arraylist传递给Java。使用JStrings(getQuestionDetails)传递JArray)可以完美地工作(用Rust传递它们,用Java读取它们),我已经能够让它在添加和初始化整数时构建Arraylist(getArrayList),但我就是不知道如何添加字符串和正确初始化它 我知道,我的代码不能与此职位(文档:)所需的JValue一起工作,我需要传递JString。因此,编译器告诉我: erro

我有一个未知大小的rust向量,它只填充了字符串,我试图用JNI板条箱将其作为
Arraylist
传递给Java。使用
JStrings
getQuestionDetails
)传递
JArray
)可以完美地工作(用Rust传递它们,用Java读取它们),我已经能够让它在添加和初始化整数时构建
Arraylist
getArrayList
),但我就是不知道如何添加字符串和正确初始化它

我知道,我的代码不能与此职位(文档:)所需的
JValue
一起工作,我需要传递
JString
。因此,编译器告诉我:

error[E0308]: mismatched types
   --> src/lib.rs:140:70
    |
140 |           env.call_method(arraylist, "add", "(Ljava/lang/String;)Z", &[env.new_string(
    |  ______________________________________________________________________^
141 | |                 question_details_array
142 | |                     [1]
143 | |                 .to_owned(),
144 | |             )
145 | |             .unwrap()
146 | |             .to_owned()]
    | |_______________________^ expected enum `jni::wrapper::objects::jvalue::JValue`, found struct `jni::wrapper::objects::jstring::JString`
    |
    = note: expected type `jni::wrapper::objects::jvalue::JValue<'_>`
               found type `jni::wrapper::objects::jstring::JString<'_>`
那么,如何通过字符串(
JStrings
)传递
Arraylist
?我可能在某个地方拐错了方向,非常感谢您的帮助

我[…]无法将我的
JString
转换为
JObject

报告清楚地提到,有一个


impl>获取Jobject感谢您的帮助。这就是我以前尝试过的—当尝试运行//添加项环境调用方法(arraylist,“Add”,“(Ljava/lang/String;)Z),JObject::from(&env.new_String)(问题详细信息_数组[1]。to_owned())。unwrap()。to_owned(),),)。展开();它的结果是:^^^^^^^^^^^^^^ trait
std::convert::from为什么传递引用?JObject From>中有一个
impl>,对于JObject,您肯定是对的-我将它重写为:let cls_arraylist=env.find_class(“java/util/arraylist”).unwrap();让arraylist=env.new_对象(cls_arraylist,(()O),&[]).unwrap();设muti=0;而i<7{//additems env.call_方法(arraylist,“Add”,“Ljava/lang/Object;”)Z,&[JValue::from(JObject::from(env.new_string(“PETER.to_string()).unwrap()),).unwrap();i+=1;}但现在,当它试图以Java中对象的Arraylist的形式访问它时,它会因SIGABRT而崩溃。构造函数上的签名非常可疑。返回类型应为void(V),而不是O。
#[allow(dead_code)]
mod lib_impp;

use jni::objects::{JObject, JString, JValue};
use jni::sys::{jarray, jobject, jobjectArray, jstring};
use jni::JNIEnv;
use std::convert::TryFrom;
use std::ffi::{CStr, CString};

// Return Arraylist - use Array as source for easier testing instead of Vec
#[no_mangle]
pub unsafe extern "C" fn Java_com_example_android_MainActivity_getArraylist(
    env: JNIEnv,
    _: JObject,
    question_num: i32,
    jeopardy_mode: bool,
) -> jobject {
    let question_details_array: [String; 4] =
        lib_impp::get_mc_distractors(question_num, jeopardy_mode, &FILES_PATH);
    let cls_arraylist = env.find_class("java/util/ArrayList").unwrap();
    let arraylist = env.new_object(cls_arraylist, "()String", &[]).unwrap();
    let mut i = 0;
    while i < 4 {
        // Add items
        env.call_method(
            arraylist,
            "add",
            "(Ljava/lang/String;)Z",
            &[env
                .new_string(question_details_array[1].to_owned())
                .unwrap()
                .to_owned()],
        )
        .unwrap();
        i += 1;
    }
    *arraylist
}

// Return Array with a Question Element
#[no_mangle]
pub unsafe extern "C" fn Java_com_example_android_MainActivity_getQuestionDetails(
    env: JNIEnv,
    _: JObject,
    question_num: i32,
) -> jarray {
    let question_details_array: [String; 4] =
        lib_impp::get_question_details(question_num, &FILES_PATH);
    // Initialize our array with 4 empty Strings
    let array: jobjectArray = env
        .new_object_array(
            4,
            env.find_class("java/lang/String").unwrap(),
            *env.new_string("").unwrap(),
        )
        .unwrap();
    let mut i = 0;
    while i < 4 {
        // Edit every Item of the Array to give it the values we want
        env.set_object_array_element(
            array,
            i,
            *env.new_string(
                question_details_array
                    [usize::try_from(i).expect("Variable i could not be converted to usize.")]
                .to_owned(),
            )
            .unwrap()
            .to_owned(),
        )
        .expect("Could not perform set_object_array_element on array element.");
        i += 1;
    }
    array
}