0

サーブレットからAndroidアプリへのブール値として画像を表示したい。サーブレットが Google App Engine にアップロードされます。これが私のサーブレットです。「arrBool」値は、ランダムな値のように表示されます。

///

    resp.setContentType("text/plain");

    if (req.getParameterMap().containsKey("message"))
        message = req.getParameter("message");

    resp.getWriter().println("Server Said" + message);




    for (int i=0; i<arrBool.length; i++) {
        arrBool[i] = r.nextBoolean();
            if(arrBool[i]==true) {
                resp.getWriter().print(arrBool);
            }
    }

これは私のAndroidアプリケーションファイルです:

///

RestClient client = new RestClient("http://machougul01.appspot.com/listenforrestclient");
        client.AddParam("message", "Hello two World");
//        client.AddParam("arrBool", "textView1");


    try
    {
        client.Execute(RequestMethod.GET);

    }
    catch (Exception e)
    {
        textView.setText(e.getMessage());
    }

    String response = client.getResponse();
    textView.setText(response);
}

出力には、「Server said: Hello two world」と arrBool 値:「m9a9990a m9a9990」が表示されます。 arrBool 値を m9a9990a の代わりにイメージとして設定したいです。したがって、ランダムな値が選択されている場合はいつでも、6 台中 1 ~ 6 台の車の数が表示されます。これを手伝ってください。

4

2 に答える 2

0

変更してみる

resp.getWriter().print(arrBool);

resp.getWriter().print(String.valueOf(arrBool));

応答に文字列を書き込みます。

または、これを変更できます

for (int i=0; i<arrBool.length; i++) {
        arrBool[i] = r.nextBoolean();
            if(arrBool[i]==true) {
                resp.getWriter().print(arrBool);
            }
    }

for (int i=0; i<arrBool.length; i++) {
        arrBool[i] = r.nextBoolean();
            if(arrBool[i]==true) {
                resp.getWriter().print(1);
            }
    }

したがって、クライアント側で応答を読み取ると、「Server said: Hello two world」という応答行が表示されます 111

次に、文字列を解析し、1 または true を出力して表示または表示できません。

于 2012-12-05T06:12:21.823 に答える
0

あなたがしなければならないことがいくつかあります。

まず、必要な import ステートメントがいくつかあります。base64 には apache commons を使用できるかもしれません。この例では xerces を使用しています。

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

次に、ブール値からイメージを作成する必要があります。

BufferedImage image = new BufferedImage(wid,hgt,BufferedImage.TYPE_3BYTE_RGB);

final int BLACK = java.awt.Color.BLACK.getRGB();
final int WHITE = java.awt.Color.WHITE.getRGB();
for(int x = 0; x < wid; x++) {
    for(int y = 0; y < hgt; y++) {
        boolean val = array[x*wid + y];
        image.setRGB(x,y, val ? BLACK : WHITE);
    }
}

おそらくそこでバイナリ イメージを使用できたはずです。これは私が直接知っていたことです:)フォーマットを変更するためにそれをいじることができると確信しています。

次に、それをBase64に変換する必要があります

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bytes = out.toByteArray();
String imageText = Base64.encode(baos.toByteArray());

次に、その base 64 を参照する HTML を吐き出す必要があります。

String html = "<img src=\"data:image/bmp;" + imageText + "\" alt=\"Random booleans\">";

そして、その html をページに吐き出すと、準備は完了です。

于 2012-12-05T16:20:55.433 に答える