Android中的URL验证

时间:2019-06-25 18:26:46

标签: java android

我正在我的客户端和Android上的服务器(我的URL为我所知)之间建立连接。虽然,我以要求用户输入服务器URL的方式设计了我的应用程序,但我无法对其进行验证。 (即:仅在建立连接后打开一个用于访问地址空间的窗口)

我正在使用异步任务来建立连接。

请在这里引导我。 Android和Java的新手

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
textViewForURL = (TextView)findViewById(R.id.textViewForURL); // user inputs URL
connectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(textViewForURL.getText().toString().matches("")){
              // setup the alert builder
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Warning!");
                    builder.setMessage("Endpoint URL cannot be Null");
                    // add a button
                    builder.setPositiveButton("OK", null);
                    // create and show the alert dialog
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else {

                    ConnectAndReadTask task = new ConnectAndReadTask();
                    task.execute();}}}

异步任务如下

        public ConnectAndReadTask(){
        }
        @Override
       public UaClient doInBackground(Void... params) {
            client = null;
            String result = null;
            try {
                String URL = textViewForURL.getText().toString();
                System.out.println("Connecting...");
                client = new UaClient(URL);
ApplicationDescription appDescription = new ApplicationDescription();
                appDescription.setApplicationName(new LocalizedText("SimpleAndroidClient",
                        Locale.ENGLISH));
                String android_id = Settings.Secure.getString(
                        getApplicationContext().getContentResolver(),
                        Settings.Secure.ANDROID_ID);
                appDescription.setApplicationUri("urn:" + android_id + ":UA:SimpleAndroidClient");
                appDescription.setProductUri("urn:prosysopc.com:UA:SimpleAndroidClient");
                appDescription.setApplicationType(ApplicationType.Client);

                PkiFileBasedCertificateValidator validator = new PkiFileBasedCertificateValidator(
                        getFilesDir().getPath() + "/PKI/CA");
                validator.setValidationListener(new CertificateValidationListener() {
                    @Override
                    public PkiFileBasedCertificateValidator.ValidationResult onValidate(
                            Cert cert, ApplicationDescription applicationDescription,
                            EnumSet<PkiFileBasedCertificateValidator.CertificateCheck> enumSet) {
                        return PkiFileBasedCertificateValidator.ValidationResult.AcceptPermanently;
                    }
                });
                client.setCertificateValidator(validator);
                ApplicationIdentity identity=ApplicationIdentity.loadOrCreateCertificate(
                        appDescription, "Sample Organisation", "opcua",
                        new File(validator.getBaseDir(), "private"), true);
                identity.setApplicationDescription(appDescription);
                client.setApplicationIdentity(identity);
client.setTimeout(60000);
                client.setSecurityMode(SecurityMode.NONE);
                client.setUserIdentity(new UserIdentity());
                System.out.println("Lets connect");
                client.connect();
 } catch (Exception e) {
                result = e.toString();
            }
        }return client;  }}}

0 个答案:

没有答案