Java Android/Eclipse在插件中打开MainActivity之外的文件
我的Android Phonegap应用程序有问题 我创建了一个插件,将数据从HTML/JAVASCRIPT发送到Java,Java会将这些数据发送到 使用HTTPS post的服务器 要获得这项工作,我需要从我的资产文件夹中打开一个ssl.crt(certification) 在Cordova类中,此函数起作用,因为它扩展了Cordova活动 我的插件类:公共类ConnectPlugin扩展了CordovaPlugin 以下是登录方法:Java Android/Eclipse在插件中打开MainActivity之外的文件,java,android,eclipse,file,cordova,Java,Android,Eclipse,File,Cordova,我的Android Phonegap应用程序有问题 我创建了一个插件,将数据从HTML/JAVASCRIPT发送到Java,Java会将这些数据发送到 使用HTTPS post的服务器 要获得这项工作,我需要从我的资产文件夹中打开一个ssl.crt(certification) 在Cordova类中,此函数起作用,因为它扩展了Cordova活动 我的插件类:公共类ConnectPlugin扩展了CordovaPlugin 以下是登录方法: protected String tryLogi
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()方法。
在我的main类中,obove代码100%正常工作,并向我的服务器发送请求。
但不在我的插件类中。使用
cordova.getActivity().getAssets().open("ssl.crt"));