无法在mqtt经纪人上发布消息

时间:2018-10-14 09:00:27

标签: android mqtt

我正在开发需要实现mqtt的移动应用程序。我需要将数据发布到mqtt代理,但是当我尝试发布图像数据(它是图像的字节数组)时,它总是无法发布。

发布时它没有显示任何错误,但是当我尝试发布消息时,它没有在代理上发布任何数据。仅当从有效载荷(即pichex)中删除图像数据时,才能发布该消息。

在mqtt代理上发布的每个数据都需要转换为十六进制ascii代码。在发布到代理之前,图像的字节数组还需要转换为十六进制ascii字符串代码,这是项目的要求。

private void publish(){
    String command = "000000";
    String reserve = "303030303030303030303030303030303030303030303030";
    String tag = "tag";

    name = eTname.getText().toString();
    price = eTprice.getText().toString();
    type = TypeS.getSelectedItem().toString();
    desc = eTdesc.getText().toString();

    userid = MainActivity.getGetuserid();

    String namehex = toHexString(name);
    String pricehex = toHexString(price);
    String typehex = toHexString(type);
    String deschex = toHexString(desc);
    String useridhex = toHexString(userid);

    Bitmap PicBitmap = ((BitmapDrawable)iVpic.getDrawable()).getBitmap();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PicBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] picbyte = bos.toByteArray();

    String pichex = bytesToHex(picbyte);

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
    Date currentTime = Calendar.getInstance().getTime();
    String currentTimeString = df.format(currentTime);

    String payload = "{command:" + command + ", reserve:" + reserve + ", Client ID:" + serverConnectTo + ", Posted name:" + namehex +
            ", Posted Price:" + pricehex + ", Posted Type:" + typehex + ", Posted Description:" + deschex
            + ", Posted Picture:"  + pichex + ", Date Time Posted:"  + currentTimeString +  ", User ID:" + useridhex + "}";

    byte[] encodedPayload = new byte[0];

    try {
        encodedPayload = payload.getBytes();
        MqttMessage message = new MqttMessage(encodedPayload);
        message.setQos(qos);
        mqttClient.publish(topic, message);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}
   public static String toHexString(String input) {
    return String.format("%x", new BigInteger(1, input.getBytes()));
}

public static String bytesToHex(byte[] in) {
    final StringBuilder builder = new StringBuilder();
    for(byte b : in) {
        builder.append(String.format("%02x", b));
    }
    return builder.toString();
}

需要发布的图片的十六进制字符串值为:

89504e470d0a1a0a0000000d49484452000005dc0000046508020000002ce5923a0000000373424954080808dbe14fe00000200049444154789cecbd617723c971b41b91550dcecaf6bde7feff3ff99ed7d60e89ae8cb81fb21a046738eb5dd9d648de7c3ca74d8140a3d16842aa4064046da3699aa6699aa6699aa6699aa6f9fb123ffb009aa6699aa6699aa6699aa669fe8cb428d3344dd3344dd3344dd3344df3136851a6699aa6699aa6699aa6699ae627d0a24cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f408b324dd3344dd3344dd3344dd3343f8116659aa6699aa6699aa6699aa6697e022dca344dd3344dd3344dd3344dd3fc045a94699aa6699aa6699aa6699aa6f909b428d3344dd3344dd3344dd3344df3136851a6699aa6699aa6699aa6699ae627d0a24cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f408b324dd3344dd3344dd3344dd3343f8116659aa6699aa6699aa6699aa6697e022dca344dd3344dd3344dd3344dd3fc045a94699aa6699aa6699aa6699aa6f909b428d3344dd3344dd3344dd3344df3136851a6699aa6699aa6699aa6699ae627d0a24cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f408b324dd3344dd3344dd3344dd3343f8116659aa6699aa6699aa6699aa 6697e022dca344dd3344dd3344dd3344dd3fc045a94699aa6699aa6699aa6699aa6f909b428d3344dd3344dd3344dd3344df3136851a6699aa6699aa6699aa6699ae627d0a24cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f408b324dd3344dd3344dd3344dd3343f8116659aa6699aa6699aa6699aa6697e022dca344dd3344dd3344dd3344dd3fc045a94699aa6699aa6699aa6699aa6f909b428d3344dd3344dd3344dd3344df3136851a6699aa6699aa6699aa6699ae627d0a24cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f408b324dd3344dd3344dd3344dd3343f8116659aa6699aa6699aa6699aa6697e02f3671f40d3344dd334efd8fef476927fe723699aa6699aa669fea769a74cd3344dd3344dd3344dd334cd4fa04599a6699aa6699aa6699aa6699a9f007f64936e9aa6699ae6bfceffd6ff9eed71aaa6699aa6699aff3aed94699aa6699aa6699aa6699aa6f90974d06fd3344dd3fc0ff2479d323f72a0fcb3386eda41d3344dd3344df3fb69a74cd3344dd3344dd3344dd334cd4fa09d324dd334cd3f0e02e2f76dffd1d00f6eff8d4315003300c00100140d00bcf656de1813340002a0f77fbcb6a2c3fce4fcf0a3b1c6bfcbbff2cd7eb09ff53b0c00e3e36dff2c0699c73bf5d3afa2ffeaf56c80bf7bfb47f9677 93b9ba6699ae69f9d16659aa6699a7f08840404f8f76d11df8a028f1d95b0f1ad4a221b1f876bc881f7b1a06f17a13f9ac29116be1fd2a1ae95efd3fe4100b157dcdf2cbc251880616094624204692851a7822421844192b4e120609990ec12659c701808d2e​​400b49f58d8c7493e6b460fa1e71b02248c0fff0000593bfab0ae37c8083fbda8c770d5b75356b46d40833f941e6c5fe7874f0fff5ce7fad178d456b21c00fce15dae03defb34b27efa8342c88feffefb658f6faf493d6d7fdf113d092d3244c477dbf10321f38f427faecbf4745ad3344dd3fcf7d2a24cd3344df30f857edff68faca9fd2e95d87eacea9f7ffee4417e3cf8039f3c84c256165442cfd32373bb60b616f05826bb9ec11fc50e1ab400818265c4a5cb2004386107606400b658af2261264823499a2aed263c002088787f9eefe598ebf4d01f45193e8e09803faceb83012760204a5279fc3ee2c9cb53f7787e033e39cf7efaf947f7fa6fe1f1127eeb78fe30bcb67fece0bfbfaa8bdff4ce3c2ee33ff4547f137c1719bf77de344dd3344df3df468b324dd334cd3f0e7f406a11fcae1becdb1ede930f8e8beb37d7ddf97cf3e507f86e99cb3fb8f265d9771e5acef5033fc82e4f7e1f8b60c0861d0410cfde0463ab29306cc35652a8bb03591a90edeb1cc80049030eda41879d00288204b7c981e5d7783a09fba c6c4bcf77ee0a7d761e9c008cbc5e65c0b1c7a6f6d68008d4d604307ec3b4719d2e3f7efe1b0383b71b259e5ed96ff007aeb73ae1df8b26efc7c9efefffed2e9eee5f7b90afcb8cef27fac329faccb0c2ba3900e2fb2d3fbdfd37f9d64f435c7f5d0e501fb6faee96921da3c59aa6699aa6f95b6851a6699aa6f987803f5842fb7371247e674acab593a79f7fd320f37b77e86fa496bde4f677a2ccb5d6fd60a588e774182cba06a904972d0690418009b0e68a980b166c486599b10540a897a3c7cb34610719b1a36a68925b9121be1365ea883476bacd9553b32d323f3c57ba13a083e425338579e5e3f83a1ebbb4a2271bcea767d2d710d3b61d919fff4f941fb7503d396ef0dff0167f47d997f0b40d50f68f148fff5cf1f19308755dcef9b4c565aafab85b1a105d7a5b001fb6c02004c4c7ed374ffc24a93c5edd472ebd53bf77fba371c2a6699aa6697e9316659aa6699a7f107eb488fdcf1d2b359cb3e76cfce497f96617bf31b8c4672fcdfb7e2ebe5db2fa7dd90c97c5410abc4b3397b1e2c10727821e712c1610e6b96f96390c0b346c7098212060e55bd8b56b695d4f7165a9ec85f17ed5368023af27211911d8f932789c193dfb6554d136212a1c8f8c924fd40d07a859278db1137e38f6e9535624f1f5300134e23de7e69b9dbdcf8925ec876664e71f0c

This is the image that need to publish

图片的大小约为100KB。

仅当从有效载荷(即pichex)中删除图像数据时,才能发布消息:

String payload = "{command:" + command + ", reserve:" + reserve + ", Client ID:" + serverConnectTo + ", Posted name:" + namehex + ", Posted Price:" + pricehex + ", Posted Type:" + typehex + ", Posted Description:"  + deschex + ", Date Time Posted:"  + currentTimeString +  ", User ID:" + useridhex + "}";

如果删除了pichex,则订户可以接收该消息:

{command:000000, reserve:303030303030303030303030303030303030303030303030, Client ID:Server, Posted name:627572676572, Posted Price:392e3939, Posted Type:6e6f6f646c65, Posted Description:616a6468, Date Time Posted:2018-10-14 08.09.29, User ID:696b6f6d61}

0 个答案:

没有答案