简体   繁体   中英

How to run java method in react-native

I have an already working Java method that I want to get the result of the method in a react-native component. I don't really know anything about java, but I know this code works. Is there anyway I could run this java method in any javascript file.

// Java File

import com.twilio.jwt.accesstoken.AccessToken;
import com.twilio.jwt.accesstoken.VideoGrant;

  public class Main {

// Substitute your Twilio AccountSid and ApiKey details
    public static final String ACCOUNT_SID = "ACCOUNT_SID";
    public static final String API_KEY_SID = "API_KEY_SID";
    public static final String API_KEY_SECRET = "API_KEY_SECRET";

    public static void main(String[] args) throws Exception {
    // Create a VideoGrant
        final VideoGrant grant = new VideoGrant();
        grant.setRoom("cool room");

    // Create an Access Token
        final AccessToken token = new AccessToken.Builder(ACCOUNT_SID, API_KEY_SID, API_KEY_SECRET)
        .identity("example-user") // Set the Identity of this token
        .grant(grant) // Grant access to Video
        .build();

    // Serialize the token as a JWT
    final String jwt = token.toJWT();
    System.out.println(jwt);
  }
}

I'm assuming that piece of code you've shown is actually Android specific. In that case you will need to send the result of your Java method back to the Javascript side by using a Native Module. The official docs provide a decent example: https://facebook.github.io/react-native/docs/native-modules-android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM