gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:29:51 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com