1

这是我目前的 Midlet

 if (display.getCurrent() == mainform) {

            selectedparam = activity.getString(activity.getSelectedIndex());

            url = "http://localhost:8080/TOMCATServer/RetrieveServlet?";
            parameter = "activity=" + selectedparam;



            System.out.println(url + parameter);

            try {
                hc = (HttpConnection) Connector.open(url + parameter);
                hc.setRequestMethod(HttpConnection.POST);
                hc.setRequestProperty("CONTENT_TYPE", "application/x-www-from-urlencoded");
                hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");

                out = hc.openOutputStream();
                byte[] postmsg = parameter.getBytes();
                for (int i = 0; i < postmsg.length; i++) {
                    out.write(postmsg[i]);
                }
                out.flush();
                in = hc.openInputStream();


                int ch;
                while ((ch = in.read()) != -1) {
                    b.append((char) ch);
                }

                String result = b.toString().trim();

                System.out.println(result);
                activitiesform.deleteAll();
                activitiesform.append(result);
                display.setCurrent(activitiesform);


            } catch (Exception c) {
                Alert alert = new Alert("Error", "The connection has failed. Please try again.", null, AlertType.ERROR);
                display.setCurrent(alert);
            }

这是我当前的 Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String activityparam = request.getParameter("activity");
        String[] sports = new String[5];
        sports[0] = ("Football competition");

        if (activityparam.equals("Sports")) {
            out.println("These are the list of sporting activities \n");
            for (int i = 0; i < 5; i++) {

                out.println(sports[i]);
                //Wanted to output the images of different sports here
   }

我想要实现的是,在发送查询请求后,Servlet 可以将图像连同 sports[i] 字符串一起显示回 Midlet。现在,它只使用 PrintWriter 处理字符串文本。图像文件存储在本地,因此绝对路径应该没问题。请帮我。谢谢。

4

1 回答 1

2

I think you should go with two Servlets: one for Strings and another for images (one at a time).

MIDlet could retrieve an image with:

    Image img = Image.createImage(in);

If you need to cache image data, you can use:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buff[] = new byte[1024];
    int len = in.read(buff);

    while (len > 0) {
        baos.write(buff, 0, len);
        len = in.read(buff);
    }
    baos.close();
    buff = baos.toByteArray();
    // write to RecordStore
    Image img = Image.createImage(new ByteArrayInputStream(buff));

于 2012-04-09T20:19:46.720 回答