Displaying More string on Logcat

╄→尐↘猪︶ㄣ 提交于 2019-11-27 18:08:22
Karussell

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);
}
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.

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);
    }
}
maid450

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

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!