本文介绍了小程序URLConnection.connect从JavaScript调用失败后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java小网站和小程序需要连接到我的服务器。
这在JApplets @覆盖的init(),但不是在我自己的职能正在由JavaScript调用。

I have a website with a Java Applet and that applet needs to connect to my server.This works in JApplets @Override init() but not in my own functions that are being called by javascript.

final URL url = new URL(SERVER_URL + action);
System.out.println("url:" + url);
System.out.println("postString: " + postString);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(!postString.isEmpty()) {
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", Integer.toString(postString.getBytes().length));
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    System.out.println("connecting...");
    connection.connect(); // same with connection.getOutputStream();
    System.out.println("connected");
    ....

网站

<a href="javascript: document.applet.update();" title="update from server">Update</a>
<applet id="applet" archive="/public/Antucation-0.1.0.jar?random=3765332307555812156" code="de.antucation.controller.Controller.class" width="100%" height="800px">
<param name="colonyId" value="1">
</applet>

输出:

url:http://localhost:9000/applet/showCode
postString: colonyId=1
connecting...

我身边有一试捕了System.out的呼叫,但没有任何反应有两种。
然而,这是绝对的作品罚款:

I have a try catch around it with a System.out call but nothing happens there either.However this works absolutly fine:

@Override
public void init() {
    update();
}

哦,当然还有小程序也来自的http://本地主机:9000 /

我要如何解决这个或者修复它?

How do I work around this or fix it?

推荐答案

尝试沿着线的东西:

public void callFromJavaScript(final String param) {
    AccessController.doPrivileged( new PrivilegedAction<Void>() {
        public Void run() {
            // call code to make the connection..
            return null;
        }
    });
}

这篇关于小程序URLConnection.connect从JavaScript调用失败后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 00:17