LWUIT IO ConnectionRequest:POST请求,如何获取响应头?

发布于 2025-01-04 14:12:25 字数 2609 浏览 2 评论 0原文

我发送 POST 请求以在服务器上创建一些数据资源。我收到响应代码 201 - 一切正常。但我还需要从响应中获取 2 个标头。这些标头包含所创建资源的属性。我没有找到如何使用 ConnectionRequest 的 API 来做到这一点。

该类具有 readHeaders(connection) 和 getHeader(connection) 受保护方法。但当我收到回复时我无法使用它们 - 出现异常。

我的代码示例如下所示:

ConnectionRequest reqresp = new ConnectionRequest () {
    protected  void buildRequestBody (java.io.OutputStream os) {
    Logger.inst ().write ("buildRequestBody");
        final String body = "Dummy Request Body"; // necessary for my request
        try {
            os.write (body.getBytes () );
        }
        catch (Exception ex) {
            final String errMsg = ex.toString ();
            Logger.inst ().write (errMsg);
            throw new RuntimeException (errMsg);
        }
    }

    protected void readResponse (InputStream input) throws IOException  {
        // Actually this method will not be called.
        Logger.inst ().write ("readResponse");
        String respText;
        try {
            respText = Util.readToString (input);
        }
        catch (Exception ex) {
            final String errMsg = ex.toString ();
            Logger.inst ().write (errMsg);
            throw new RuntimeException (errMsg);
        }
        Logger.inst ().write (respText);
    }
};
reqresp.setUrl ("MY SERVER URL");
reqresp.setPriority (ConnectionRequest.PRIORITY_HIGH);
reqresp.setPost (true);
reqresp.addRequestHeader ("header1", "val1");
reqresp.addRequestHeader ("Content-type", "application/json");
reqresp.addResponseCodeListener (new ActionListener () {
        public void actionPerformed (ActionEvent ae) {
            Logger.inst ().write ("ResponseCodeListener:");
            Logger.inst ().write (ae.toString () );
            if (ae instanceof NetworkEvent) {
                NetworkEvent evt = (NetworkEvent)ae;
                Logger.inst ().write ("message: " + evt.getMessage () );
                Logger.inst ().write ("response code: " + evt.getResponseCode () );
            }
        }
    });
reqresp.addResponseListener (
    new ActionListener () {
        public void actionPerformed (ActionEvent ae) {
            Logger.inst ().write ("ResponseCodeListener:");
            Logger.inst ().write (ae.toString () );
            if (ae instanceof NetworkEvent) {
                NetworkEvent evt = (NetworkEvent)ae;
                Logger.inst ().write ("message: " + evt.getMessage () );
                Logger.inst ().write ("response code: " + evt.getResponseCode () );
            }
        }
    }
);

NetworkManager.getInstance ().addToQueue (reqresp);

I send POST request to create some data resource on server. I get Response Code 201 - all is OK. But I also need to get 2 headers from the response. Those headers contain attributes of the created resource. I did not find the way how to do it using ConnectionRequest's API.

The class has readHeaders(connection) and getHeader(connection) protected methods. But I could not use them when I got the response - got an exception.

My code example is shown below:

ConnectionRequest reqresp = new ConnectionRequest () {
    protected  void buildRequestBody (java.io.OutputStream os) {
    Logger.inst ().write ("buildRequestBody");
        final String body = "Dummy Request Body"; // necessary for my request
        try {
            os.write (body.getBytes () );
        }
        catch (Exception ex) {
            final String errMsg = ex.toString ();
            Logger.inst ().write (errMsg);
            throw new RuntimeException (errMsg);
        }
    }

    protected void readResponse (InputStream input) throws IOException  {
        // Actually this method will not be called.
        Logger.inst ().write ("readResponse");
        String respText;
        try {
            respText = Util.readToString (input);
        }
        catch (Exception ex) {
            final String errMsg = ex.toString ();
            Logger.inst ().write (errMsg);
            throw new RuntimeException (errMsg);
        }
        Logger.inst ().write (respText);
    }
};
reqresp.setUrl ("MY SERVER URL");
reqresp.setPriority (ConnectionRequest.PRIORITY_HIGH);
reqresp.setPost (true);
reqresp.addRequestHeader ("header1", "val1");
reqresp.addRequestHeader ("Content-type", "application/json");
reqresp.addResponseCodeListener (new ActionListener () {
        public void actionPerformed (ActionEvent ae) {
            Logger.inst ().write ("ResponseCodeListener:");
            Logger.inst ().write (ae.toString () );
            if (ae instanceof NetworkEvent) {
                NetworkEvent evt = (NetworkEvent)ae;
                Logger.inst ().write ("message: " + evt.getMessage () );
                Logger.inst ().write ("response code: " + evt.getResponseCode () );
            }
        }
    });
reqresp.addResponseListener (
    new ActionListener () {
        public void actionPerformed (ActionEvent ae) {
            Logger.inst ().write ("ResponseCodeListener:");
            Logger.inst ().write (ae.toString () );
            if (ae instanceof NetworkEvent) {
                NetworkEvent evt = (NetworkEvent)ae;
                Logger.inst ().write ("message: " + evt.getMessage () );
                Logger.inst ().write ("response code: " + evt.getResponseCode () );
            }
        }
    }
);

NetworkManager.getInstance ().addToQueue (reqresp);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2025-01-11 14:12:25

ConnectionRequest 中尝试类似的操作:

protected void readHeaders(Object connection) throws IOException {
    String val = getHeader(connection, "MyHeaderName");
}

Try something like this in the ConnectionRequest:

protected void readHeaders(Object connection) throws IOException {
    String val = getHeader(connection, "MyHeaderName");
}
巴黎盛开的樱花 2025-01-11 14:12:25

我无法使用 LWUIT 的 IO 执行该任务。但我使用 MIDP 的 HTTPConnection 轻松做到了这一点,并且没有遇到任何问题。

关于 MIDP 的 HTTPConnection API 的一点小评论。这是同步的。所以,我不想在主 GUI 线程中使用它。此外,API 是低级的。因此,我必须实现某种包装器来隐藏细节并在单独的线程中异步执行 HTTP 请求。

现在评论一下 LWUIT 的 IO ConnectionRequest 类。我发现它对程序员不友好。它既代表请求又代表响应。因此,尚不清楚哪些方法与请求相关,哪些方法与响应相关。

I could not perform the task using the LWUIT's IO. But I did it easily using the MIDP's HTTPConnection and met no problems.

One small comment about the MIDP's HTTPConnection API. It's synchronous. So, I did not want to use it in the main GUI thread. Moreover, the API is low-level. So, I had to implement some kind of wrapper to hide the details and to execute HTTP request asynchronously in a separate thread.

Now one comment about the LWUIT's IO ConnectionRequest class. I found it programmer-unfriendly. It represents both a request and response. So, it's not clear what the methods are related to the request and what to the response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文