简体   繁体   English

Concat 2字符串erlang并使用http发送

[英]Concat 2 strings erlang and send with http

I'm trying to concat 2 variables Address and Payload. 我正在尝试合并2个变量Address和Payload。 After that I want to send them with http to a server but I have 2 problems. 之后,我想使用http将它们发送到服务器,但是有2个问题。 When i try to concat the 2 variables with a delimiter ';' 当我尝试用定界符';'连接2个变量时 it doesn't work. 它不起作用。 Also sending the data of Payload or Address doesn't work. 也无法发送有效载荷或地址数据。 This is my code: 这是我的代码:

handle_rx(Gateway, #link{devaddr=DevAddr}=Link, #rxdata{port=Port, data= RxData }, RxQ)->
    Data = base64:encode(RxData),
    Devaddr = base64:encode(DevAddr),
    TextAddr="Device address: ",
    TextPayload="Payload: ",
    Address = string:concat(TextAddr, Devaddr),
    Payload = string:concat(TextPayload, Data),
    Json=string:join([Address,Payload], "; "),
    file:write_file("/tmp/foo.txt", io_lib:fwrite("~s.\n", [Json] )),
    inets:start(),
    ssl:start(),
    httpc:request(post, {"http://192.168.0.121/apiv1/lorapacket/rx", [], "application/x-www-form-urlencoded", Address },[],[]),
    ok;
handle_rx(_Gateway, _Link, RxData, _RxQ) ->
    {error, {unexpected_data, RxData}}.

I have no errors that I can show you. 我没有错误可以告诉您。 When I write Address or Payload individually to the file it works but sending doesn't work... 当我分别将Address或Payload写入文件时,它可以工作,但是发送不起作用...

Thank you for your help! 谢谢您的帮助!

When i try to concat the 2 variables with a delimiter ';' 当我尝试用定界符';'连接2个变量时 it doesn't work. 它不起作用。

5> string:join(["hello", <<"world">>], ";").
[104,101,108,108,111,59|<<"world">>]

6> string:join(["hello", "world"], ";").    
"hello;world"

base64:encode() returns a binary, yet string:join() requires string arguments. base64:encode()返回二进制,而string:join()需要字符串参数。 You can do this: 你可以这样做:

7> string:join(["hello", binary_to_list(<<"world">>)], ";").
"hello;world"

Response to comment : 对评论的回应

In erlang the string "abc" is equivalent to the list [97,98,99] . 在erlang中,字符串"abc"等同于列表[97,98,99] However, the binary syntax <<"abc">> is not equivalent to <<[97,98,99]>> , rather the binary syntax <<"abc">> is special short hand notation for the binary <<97, 98, 99>> . 但是,二进制语法<<"abc">> 等于<<[97,98,99]>> ,而是二进制语法<<"abc">>是二进制<<97, 98, 99>>特殊缩写形式<<97, 98, 99>>

Therefore, if you write: 因此,如果您写:

Address = [97,98,99].

then the code: 然后是代码:

Bin = <<Address>>.

after variable substitution becomes: 在变量替换变成:

Bin = <<[97,98,99]>>.

and that isn't legal binary syntax. 那不是合法的二进制语法。

If you need to convert a string/list contained in a variable, like Address, to a binary, you use list_to_binary(Address) --not <<Address>> . 如果需要将包含在变量中的字符串/列表(例如Address)转换为二进制,请使用list_to_binary(Address) <<Address>>

In your code here: 在您的代码中:

Json = string:join([binary_to_list(<<Address>>),
                    binary_to_list(<<Pa‌​yload>>)], 
                    ";").

Address and Payload were previously assigned the return value of string:concat() , which returns a string, so there is no reason to (attempt) to convert Address to a binary with <<Address>> , then immediately convert the binary back to a string with binary_to_list() . 先前已为Address和Payload分配了返回值string:concat() ,该返回值返回一个字符串,因此没有理由(尝试)使用<<Address>>将Address转换为二进制,然后立即将二进制转换回一个带有binary_to_list()的字符串。 Instead, you would just write: 相反,您只需编写:

Json = string:join(Address, Payload, ";")

The problem with your original code is that you called string:concat() with a string as the first argument and a binary as the second argument--yet string:concat() takes two string arguments. 原始代码的问题在于,您调用string:concat() ,将字符串作为第一个参数,将二进制文件作为第二个参数,但string:concat()需要两个字符串参数。 You can use binary_to_list() to convert a binary to the string that you need for the second argument. 您可以使用binary_to_list()将二进制文件转换为第二个参数所需的字符串。

Sorry I'm new to Erlang 抱歉,我是Erlang的新手

As with any language, you have to study the basics and write numerous toy examples before you can start writing code that actually does something. 与任何语言一样,您必须先学习基础知识并编写大量玩具示例,然后才能开始编写实际上可以完成某些工作的代码。

You don't have to concatenate strings. 您不必连接字符串。 It is called iolist and is one of best things in Erlang: 它被称为iolist ,是Erlang中最好的东西之一:

1> RxData = "Hello World!", DevAddr = "Earth",
1> Data = base64:encode(RxData), Devaddr = base64:encode(DevAddr),
1> TextAddr="Device address", TextPayload="Payload",
1> Json=["{'", TextAddr, "': '", Devaddr, "', '", TextPayload, "': '", Data, "'}"].
["{'","Device address","': '",<<"RWFydGg=">>,"', '",
 "Payload","': '",<<"SGVsbG8gV29ybGQh">>,"'}"]
2> file:write_file("/tmp/foo.txt", Json).
ok
3> file:read_file("/tmp/foo.txt").
{ok,<<"{'Device address': 'RWFydGg=', 'Payload': 'SGVsbG8gV29ybGQh'}">>}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM