Hi,
I can resolve my problem. I 'm use JSON in twow parts (server and client(J2ME)).
I just use Hessian to return the String..

Follow the code

Hessian Server:

public String downloadProdutos() throws Exception {

        List ret = ProdutoBO.getInstance().findAllProduto();

        // JSON return
        JSONArray jsonArray = new JSONArray();

        if (ret.size() > 0) {

            for(Iterator iter = ret.iterator(); iter.hasNext();) {
                Produto prod = (Produto) iter.next();

                // create object JSON
                JSONObject jsonObj = new JSONObject();

                jsonObj.put("codigo", prod.getCodigo());
                jsonObj.put("descricao", prod.getDescricao());
                jsonObj.put("id", prod.getId());
                jsonObj.put("importado", prod.getImportado());
                jsonObj.put ("precoCompra", prod.getPrecoCompra());
                jsonObj.put("precoVenda", prod.getPrecoVenda());
                jsonObj.put("tipoOrigem", prod.getTipoOrigem());

                // Add json object to array.
                jsonArray.put(jsonObj);
            }

            // Just return the array from JSON
            return jsonArray.toString();
        }
        else {
            return "";
        }
    }

------------------------------
--------------------------

Code in the client (Pocket PC - J2ME/CDC)

    public void showPage(String pageName, Thinlet thinlet) {

        Object senha = thinlet.find("txtSenha");

            try {
                // Call web Service Hessian
                String url =
"http://192.168.0.101:8080/br.com.rosaFlores.WebService/test ";

                HttpConnection      c = (HttpConnection) Connector.open(url);
                c.setRequestMethod(HttpConnection.POST);

                OutputStream        os = c.openOutputStream();
                MicroHessianOutput  out = new MicroHessianOutput(os);

                out.startCall("downloadProdutos");

                out.completeCall();
                os.flush();

                // Begining read return
                InputStream is = c.openInputStream();

                MicroHessianInput in = new MicroHessianInput(is);
                in.startReply();

                // Get return JSON from Hessian
                String respJSON = in.readString();

                // Close Web Service read
                in.completeReply();

                // Deserialize JSON -> POJO
                JSONArray jsonArray = new JSONArray(respJSON);

                for(int item = 0; item < jsonArray.length(); item++) {
                    // Get JsonObject
                    JSONObject jsonObj = jsonArray.optJSONObject(item);

                    // JSON -> POJO
                    Produto prod = new Produto();

                    prod.setCodigo(jsonObj.getString("codigo"));
                    prod.setDescricao(jsonObj.getString ("descricao"));
                    prod.setId(Integer.valueOf(jsonObj.getInt("id")));

prod.setImportado(Boolean.valueOf(jsonObj.getString("importado")));

prod.setPrecoCompra(BigDecimal.valueOf(jsonObj.getLong("precoCompra")));

prod.setPrecoVenda(BigDecimal.valueOf(jsonObj.getLong("precoVenda")));
                    prod.setTipoOrigem (jsonObj.getString("tipoOrigem"));
                }

                thinlet.setString(senha, "text", "sucess: " +
jsonArray.length());

            } catch (Exception ex) {
                thinlet.setString(senha, "text", "ex: " + ex.getMessage ());
            }
    }

----------
Fábio Pinheiro
Jundiaí - SP - Brasil


_______________________________________________
hessian-interest mailing list
[email protected]
http://maillist.caucho.com/mailman/listinfo/hessian-interest

Reply via email to