java - Android/Eclipse 在插件中打开 MainActivity 之外的文件

标签 java android eclipse file cordova

我的 Android Phonegap 应用程序有问题。

我创建了一个插件来将数据从 HTML/JAVASCRIPT 发送到 Java,Java 会将此数据发送到 带有 HTTPS post 的服务器。

要获得此 Worke,我需要从我的 Assets 文件夹中打开一个 ssl.crt(证书)。

在 Cordova 类中,此函数起作用是因为它扩展了 CordovaActivity。

我的插件类:public class ConnectPlugin extends CordovaPlugin

这是登录方法:

    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;


}

现在的问题是“未定义 ConnectPlugin 类型的方法 getAssets()”。

我无法在主类之外使用 getAssets() 方法。 在我的 MainClass 中,上面的代码可以 100% 正常工作并向我的服务器发送请求。 但不在我的插件类中。

最佳答案

使用

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

关于java - Android/Eclipse 在插件中打开 MainActivity 之外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456070/

相关文章:

Java JDBC MySQL异常: “Operation not allowed after ResultSet closed”

android - 从不扩展 Activity 的类开始服务

eclipse - 在 war 库中热部署资源

java - 连接Eclipse到Docker容器进行远程调试

java - 无法在 eclipse 中将项目 facet ear 的版本更改为 1.3 maven 错误?

java - Tomcat 服务器工作,但无法部署我的 JSP 文件

java - 错误: cannot find symbol: class FileNotFoundException

JAVA:将 BigInteger 转换为数字数组

android - 如何在 Android 的 View 寻呼机中启用点击监听器?

android - android编辑文本数组的解析方法