System.err:TypeError:com.tns.TestObject不是构造函数

时间:2018-08-02 09:40:43

标签: java android ssl https nativescript

我正在使用Nativescript。我在C:\project\platforms\android\app\src\main\java\com\tns中创建TestObject.java并将其编写为:

package com.tns;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class TestObject {
    public void disableSSLCertificateChecking() {

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }

            @Override
            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                // Not implemented
            }
        } };

        try {
            SSLContext sc = SSLContext.getInstance("TLS");

            sc.init(null, trustAllCerts, new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

我的问题是,如何在应用程序的main.ts中调用此函数?

我尝试在TestObject.java中添加

 public String doSomething () {
        return "hello";
    }

在main.ts中写成这样:

  var test = new com.tns.TestObject(); 
  console.log(test.doSomething());

应用执行时发生错误

  

System.err:java.lang.RuntimeException:无法创建应用程序   com.tns.NativeScriptApplication:com.tns.NativeScriptException:           System.err:调用模块函数时出错           System.err:TypeError:com.tns.TestObject不是构造函数           System.err:文件:“ file:///data/data/org.nativescript.project/files/app/main.js,行:   29栏:15           System.err:StackTrace:           System.err:框架:功能:'',文件:'file:///data/data/org.nativescript.project/files/app/main.js',   行:29,列:16

有什么想法吗?

0 个答案:

没有答案