在 Logcat 上显示更多字符串

发布于 2024-10-11 13:00:25 字数 274 浏览 2 评论 0原文

我目前正在开发一个 Android 应用程序,它使用“JSON”作为服务器的响应。 通常我处理 JSON 响应。但现在我遇到了 logcat 的问题, 如果 JSON 响应字符串非常长,超过 x 个字符(我不知道 logcat 可以显示的最大字符串到底是多少),则某些 JSON 字符串丢失。

虽然它仍然可以给我输出,但我需要从服务器传输的 JSON 字符串的信息。

是否有可能在 logcat 上显示更多字符串?就像增加缓冲区或任何可以用来增加 logcat 可以显示的最大字符串长度的参数一样。

I am currently working on an android App that uses 'JSON' as response from server.
Usually I work on the JSON response. But now I have a problem with logcat,
if the JSON response string is very long , more than x character (i don't know exactly how much is the max string that could be displayed by logcat), some of the JSON string is missing.

Although it still could give me the output, I need the information on the JSON string that is transmitted from the server.

Is there any possibility to display more string on logcat? Like increasing the buffer or any parameter that I could use to increase the maximum string length that could be displayed by logcat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

属性 2024-10-18 13:00:25

丑陋但它能完成工作:

public static void longInfo(String str) {
    if(str.length() > 4000) {
        Log.i(TAG, str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.i(TAG, str);
}

Ugly but it does the job:

public static void longInfo(String str) {
    if(str.length() > 4000) {
        Log.i(TAG, str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.i(TAG, str);
}
信仰 2024-10-18 13:00:25
if(xml.length() > 4000) {
 for(int i=0;i<xml.length();i+=4000){
    if(i+4000<xml.length())
        Log.i("rescounter"+i,xml.substring(i, i+4000));
     else
        Log.i("rescounter"+i,xml.substring(i, xml.length()));
}
} else
Log.i("resinfo",xml);

我就是这样做的。

if(xml.length() > 4000) {
 for(int i=0;i<xml.length();i+=4000){
    if(i+4000<xml.length())
        Log.i("rescounter"+i,xml.substring(i, i+4000));
     else
        Log.i("rescounter"+i,xml.substring(i, xml.length()));
}
} else
Log.i("resinfo",xml);

This is how I did it.

失而复得 2024-10-18 13:00:25

我们的一些 RESTful API 返回非常长的 JSON 响应。下面是为 LogCat 设置格式的方法:

private static final int LOGCAT_MAX_LENGTH = 3950;

...

private void logLongJsonStringIfDebuggable(String s) {
    if (BuildConfig.DEBUG) {
        while (s.length() > LOGCAT_MAX_LENGTH) {
            int substringIndex = s.lastIndexOf(",", LOGCAT_MAX_LENGTH);
            if (substringIndex == -1)
                substringIndex = LOGCAT_MAX_LENGTH;
            Log.d(TAG, s.substring(0, substringIndex));
            s = s.substring(substringIndex).trim();
        }
        Log.d(TAG, s);
    }
}

Some of our RESTful APIs return very long JSON responses. Here's the method which formats them for LogCat:

private static final int LOGCAT_MAX_LENGTH = 3950;

...

private void logLongJsonStringIfDebuggable(String s) {
    if (BuildConfig.DEBUG) {
        while (s.length() > LOGCAT_MAX_LENGTH) {
            int substringIndex = s.lastIndexOf(",", LOGCAT_MAX_LENGTH);
            if (substringIndex == -1)
                substringIndex = LOGCAT_MAX_LENGTH;
            Log.d(TAG, s.substring(0, substringIndex));
            s = s.substring(substringIndex).trim();
        }
        Log.d(TAG, s);
    }
}
好听的两个字的网名 2024-10-18 13:00:25

如果您没有使用 Eclipse,或者您使用了但 @Nanne 答案对您不起作用,我只能考虑两种选择:

  1. 最好但更复杂,我想您的 JSON 是由某种“可迭代对象”(JSON 对象和/或数组),因此,您可以解析和遍历 JSON 并分别打印 LogCat 中的每个元素
  2. 更简单但也更难看,将 JSON 字符串拆分为子字符串并打印 LogCat 中的每个子字符串(您可以找到拆分 String < 的不同方法a href="https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java">此处)

编辑:另一种可能性:将 JSON 像日志一样写入 SD 卡中的文件,然后在要检查响应时检索该文件

if you are not using eclipse, or you are but @Nanne answer doesn't work for you I can only think in two alternatives:

  1. Best but more complex, I suppose your JSON is composed by some kind of "iterables" (JSON objects and/or arrays) so, you can parse and traverse the JSON and print each element in the LogCat separately
  2. Easier but also uglier, split the JSON string in substrings and print each substring in the LogCat (you can find different ways of splitting a String here)

edit: Another possibility: write the JSON to a file in SD card like a log, and then retrieve the file when you want to check the response

_蜘蛛 2024-10-18 13:00:25

为什么不从命令行使用 logcat ?

我怀疑这是否会是你所期待的,但为什么不尝试一下呢?

发出命令

./adb -e logcat

从包含 adb 的目录 。
这是针对模拟器的。
将设备的 -e 替换为 -d

why not use logcat from a command line?

I doubt whether it will be what you're expecting, but why not give it a try?

issue the command

./adb -e logcat

from the directory which has adb.
this is for emulator.
replace -e with -d for a device

刘备忘录 2024-10-18 13:00:25

一般来说,如果您想将输出输出到 Logcat 中,您应该使用“Log”命令。

示例:

Log.d(TAG,"OUTPUT"):

d = 调试
TAG = 您定义的字符串,显示在 Logcat 中“正常”输出之前的列中
OUTPUT = 你想要打印的东西

Log 是一个日志记录类,可用于将消息打印到 logcat。如果您在 DDMS 上运行 logcat(接下来介绍),您可以实时读取消息。常见的日志记录方法包括:v(String, String)(详细)、d(String, String)(调试)、i(String, String)(信息)、w(String, String)(警告)和 e(String, String) )(错误)。

有关更多信息:

http://developer.android.com/guide/developing/debug -tasks.html

编辑:

我之前提到的是,为了测试一些你不应该使用的输出:

System.out.println("JSON stuff");

你应该在代码中使用它:

// Values just as example
private static void string TAG = "+++JSON+++";
...
Log.d(TAG,"JSON stuff in here");
...

它会显示在 Logcat 中。我不知道为什么,但这不是更好的方法。
像这样,您还可以显示错误消息:

Log.e(...);

它将更改 Logcat 中输出的颜色,因此如果发生错误,您可能会更好地看到它。

In general if you want to get output into Logcat you should use the "Log" command.

An example:

Log.d(TAG,"OUTPUT"):

d = debug
TAG = a string you define, gets displayed in Logcat in the column before the "normal" output
OUTPUT = stuff u want to get printed

Log is a logging class you can use to print out messages to the logcat. You can read messages in real time if you run logcat on DDMS (covered next). Common logging methods include: v(String, String) (verbose), d(String, String) (debug), i(String, String) (information), w(String, String) (warning) and e(String, String) (error).

For further information:

http://developer.android.com/guide/developing/debug-tasks.html

EDIT:

What I ment before is, in order to test some things with outputs you shouldn't use:

System.out.println("JSON stuff");

Instead of using this you should use in your code:

// Values just as example
private static void string TAG = "+++JSON+++";
...
Log.d(TAG,"JSON stuff in here");
...

It will get displayed in Logcat. I don't exactly know why, but it't the better way.
Like this you also could display an error message:

Log.e(...);

It will change the color of your output in Logcat, so you may see it better if e.g. an error occurrs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文