简体   繁体   English

Android / Eclipse在插件中的MainActivity外部打开文件

[英]Android/Eclipse Open a file Outside the MainActivity in a Plugin

I have a problem with my Android Phonegap App. 我的Android Phonegap应用程序有问题。

I created a plugin to send Data from HTML/JAVASCRIPT to Java and Java will send this DATA to a Server with HTTPS post. 我创建了一个插件,用于将数据从HTML / JAVASCRIPT发送到Java,Java将通过HTTPS发布将该数据发送到服务器。

To get this Worke I need to Open a ssl.crt (certification) from my Asset folder. 要获得此Worke,我需要从我的Asset文件夹中打开ssl.crt(证书)。

In the Cordova Class this function dose work because it extends the CordovaActivity. 在Cordova类中,此功能有效,因为它扩展了CordovaActivity。

My Plugin Class : public class ConnectPlugin extends CordovaPlugin 我的插件课程:公共课程ConnectPlugin扩展了CordovaPlugin

Here is the Login method: 这是登录方法:

    protected String tryLogin_2(String d1) throws CertificateException, FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException 
{          

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    // From https://www.washington.edu/itconnect/security/ca/load-der.crt


    InputStream caInput = new BufferedInputStream(this.getAssets().open("ssl.crt"));

    java.security.cert.Certificate ca;
    try {
        ca = cf.generateCertificate(caInput);
    } finally {
        caInput.close();
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);




    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 

    String httpsURL = "https://URL.com";
    OutputStreamWriter request = null;
    DataInputStream response_2 = null; 
    String parameters = "1="+d1;   
    String response = null;     

    try
    {
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    con.setSSLSocketFactory(context.getSocketFactory());
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-length", String.valueOf(query.length())); 
    con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
    con.setDoOutput(true); 
    con.setDoInput(true); 

    request = new OutputStreamWriter(con.getOutputStream());
    request.write(parameters);
    request.flush();
    request.close();            
    String line = "";               
    InputStreamReader isr = new InputStreamReader(con.getInputStream());
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null)
    {
    sb.append(line + "\n");
    }
    //Response from server after login process will be stored in response variable.                
    response = sb.toString();            
    isr.close();
    reader.close();
    }
    catch(IOException e)
    {
    response = "Error";     // Error
    }
    return response;


}

The problem now is "The method getAssets() is undefined for the type ConnectPlugin". 现在的问题是“类型ConnectPlugin的方法getAssets()未定义”。

I can't use the getAssets() method outside the Main Class. 我不能在Main Class外使用getAssets()方法。 In my MainClass the obove code work 100% fine and sends a request to my server. 在我的MainClass中,过时的代码可以100%正常工作并将请求发送到我的服务器。 But not in my Plugin Class. 但不在我的插件类中。

采用

cordova.getActivity().getAssets().open("ssl.crt"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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