【问题标题】:Android/Eclipse Open a file Outside the MainActivity in a PluginAndroid/Eclipse 在插件中打开 MainActivity 之外的文件
【发布时间】:2015-02-11 21:32:45
【问题描述】:

我的 Android Phonegap 应用有问题。

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

要获得此作品,我需要从我的资产文件夹中打开一个 ssl.crt(认证)。

在 Cordova 类中,此函数有效,因为它扩展了 CordovaActivity。

我的插件类:公共类 ConnectPlugin 扩展 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% 正常工作,并向我的服务器发送请求。 但不在我的插件类中。

【问题讨论】:

    标签: java android eclipse file cordova


    【解决方案1】:

    使用

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

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 2010-09-23
      相关资源
      最近更新 更多