Session在Android XWalkView中不起作用

时间:2015-05-25 16:09:07

标签: android session webview

我使用Crosswalk的XWalkView在我的Android应用程序中显示网页。我尝试在我的网页上使用会话,但会话根本不起作用,我无法保存任何会话。

这是我初始化XWalkView的方式:

    xWalkWebView = (XWalkView)findViewById(R.id.page);
    xWalkWebView.setResourceClient(new MyResourceClient(xWalkWebView));
    xWalkWebView.load(API.BASE_URL, null);
    if(me.beesafe.beesafe.app.AppInfo.isDebug) {
        // turn on debugging
        XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);
    }

这是MyResouceClient:

class MyResourceClient extends XWalkResourceClient {
    MyResourceClient(XWalkView view) {
        super(view);
    }

    private InputStream inputStream;

    private String builtDataString() throws IOException {
        return "key=test";
    }

    private void writeDataToBuffer(ByteArrayOutputStream stream) throws IOException {
        stream.write(this.builtDataString().getBytes());
        stream.flush();
        stream.close();
    }

    private void writeOutputStream(OutputStream output, ByteArrayOutputStream buffer) throws IOException {
        byte[] bufferArray = buffer.toByteArray();
        for(int i = 0; i < bufferArray.length; i++) {
            output.write(bufferArray[i]);
        }
        output.flush();
        output.close();
    }

    public String getContentType() {
        return "application/x-www-form-urlencoded";
    }

    public String createBoundary() {
        return "--------------------"
                + Long.toString(System.currentTimeMillis(), 16);
    }

    @Override
    public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String urlStr) {
        HttpURLConnection connection = null;
        inputStream = null;
        try {
            if(urlStr.equalsIgnoreCase(API.BASE_URL)) {
                URL url = new URL(urlStr);
                connection = (HttpURLConnection) url.openConnection();

                connection.setFixedLengthStreamingMode(this.builtDataString().getBytes().length);
                connection.setUseCaches(false);
                connection.setDoInput(true);
                connection.setConnectTimeout(20 * 1000);
                connection.setReadTimeout(30 * 1000);
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", getContentType());
                connection.setRequestProperty("Content-Language", "en-US");
                connection.setRequestProperty("Connection", "keep-alive");
                DataOutputStream outputStream = new DataOutputStream(
                            connection.getOutputStream());
                ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
                writeDataToBuffer(outputBuffer);
                writeOutputStream(outputStream, outputBuffer);
                inputStream = connection.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            inputStream = null;
        } catch (IOException e) {
            e.printStackTrace();
            inputStream = null;
        } finally {
            /*if (connection != null) {
                connection.disconnect();
            }*/
        }
        if(/*inputStream != null*/urlStr.equalsIgnoreCase(API.BASE_URL)) {
            return new WebResourceResponse(null, "utf-8", inputStream);
        } else {
            return super.shouldInterceptLoadRequest(view, urlStr);
        }
    }
}

请帮忙吗?如何开启会话?

如果我只是在我的网站上使用这个PHP代码,它就不起作用:

<?php
    session_start();
    $_SESSION['test'] = 'test';
?>

$ _SESSION [&#39; test&#39;]

中没有任何内容

1 个答案:

答案 0 :(得分:0)

php会话在xwalkview中工作,你必须放session_start();在每个php文件的开头使用会话。