1

This is my current 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);
            }

And this is my current 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
   }

What I wanted to achieve is that the Servlet could display an image back to the Midlet along with the string of sports[i], after the query request is sent. Right now, it is only dealing with String texts using PrintWriter. The image file is stored locally, so an absolute path should be fine. Please help me. Thanks.

user1033038
  • 141
  • 3
  • 15

1 Answers1

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));

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22