bash 通过 ADB 在 android 广播中将 JSON 作为额外数据发送的格式不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27940118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:10:52  来源:igfitidea点击:

Sending JSON as extra data in an android broadcast via ADB gets incorrectly formatted

androidjsonbashadb

提问by bela.waeckerlig

I'm trying to send JSON data as extra on an Android broadcast wich i send via ADB to the device. But it looks like the data which gets to the device is not as expected.

我正在尝试将 JSON 数据作为额外的 JSON 数据发送到我通过 ADB 发送到设备的 Android 广播中。但看起来到达设备的数据并不像预期的那样。

What I execute:

我执行的内容:

adb shell am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver -e "notify" '{"debug": false, "title": "Application update!"}'

What I expect as extra data:

我期望的额外数据:

{"debug": false, "title": "Application update!"}

What I get as extra data:

我得到的额外数据:

"debug": false

If I send {"debug": false "title": "Application update!"}as extra data then i get {"debug": false "title": "Application update!"}as extra data (noting the missing comma). So i assume it has something to do with Brace Expansion of my bash, but turning it off does not solve the problem and escaping the braces or the comma is not working.

如果我{"debug": false "title": "Application update!"}作为额外数据发送,那么我将获得{"debug": false "title": "Application update!"}额外数据(注意缺少的逗号)。所以我认为它与我的 bash 的 Brace Expansion 有关,但将其关闭并不能解决问题并且转义大括号或逗号不起作用。

Does anybody has a clue what am I doing wrong?

有人知道我做错了什么吗?

采纳答案by bela.waeckerlig

ok, I just found a solution. I first have to enter the devices shell via adb shelland then execute am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver --es "notify" '{"debug": false, "title": "Application update!"}'

好的,我刚刚找到了解决方案。我首先必须通过进入设备外壳adb shell然后执行am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver --es "notify" '{"debug": false, "title": "Application update!"}'

回答by Jerry101

Your workaround was a huge help!

你的解决方法是一个巨大的帮助!

It indicates that the problem is in quoting through two shells (host plus Android). Shell quoting is a horrible tarpit but there's serious extra weirdness here.

它表明问题在于通过两个shell(主机加Android)进行引用。Shell 引用是一个可怕的陷阱,但这里有严重的额外奇怪之处。

After experimenting with "echo" commands, I found that adb shell's argument must be quoted as a single argument to the local shell and the JSON payload must be quoted as a single argument to the Android shell.

在对“echo”命令进行试验后,我发现 adb shell 的参数必须作为单个参数引用到本地 shell,而 JSON 负载必须作为单个参数引用到 Android shell。

Here's a general solution (and it doesn't require sprinkling \-quoting of ", !, {, and }chars in the JSON text):

这是一个通用的解决方案(它不需要在 JSON 文本中\"!{、 和}字符进行大量引用):

adb shell "am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver -e notify '"'{"debug": false, "title": "Application update!"}'"'"

Pattern: adb shell "am broadcast ... '"'JSON_TEXT'"'"

图案: adb shell "am broadcast ... '"'JSON_TEXT'"'"

The inner pair of 'marks quotes the JSON_TEXT locally while the outer pair quotes it remotely. That outer pair is in turn "-quoted.

内对'标记在本地引用 JSON_TEXT,而外对标记远程引用它。该外部对依次被"引用。

回答by Umer Farooq

This should be the structure of the adb command.

这应该是 adb 命令的结构。

adb shell "am broadcast -a YOUR_BROADCAST_INTENT_ACTION -n YOUR_APP_PACKAGE/.PATH_TO_BROADCAST_RECIEVER_CLASS.BROADCAST_RECEIVER_CLASS -e 'STRING_KEY' 'DATA HERE'"

The above command will ensure delivery of the intent to the receiver even if the string has spaces in it.

即使字符串中有空格,上述命令也将确保将意图传递给接收者。

adb shell "am broadcast -a wingoku.custom.invoking.event -n com.wingoku.root/.broadcastReceivers.MyReceiver-e 'shellCommand' 'hello world. My name is umer'"

回答by gen

i know this is an old post, but i have some thoughts.

我知道这是一个旧帖子,但我有一些想法。

after struggle to pass data through cmd and bash args, or subprocess in program, i found use base64 to encode json dumped data is extremely convenient, just base64 decode and then json loads, no worry about quote, space, tab, anything.

在努力通过 cmd 和 bash args 或程序中的子进程传递数据后,我发现使用 base64 对 json 转储数据进行编码非常方便,只需 base64 解码然后 json 加载,不用担心引号、空格、制表符等。