Displaying More string on Logcat

前端 未结 6 930
长发绾君心
长发绾君心 2020-12-04 18:10

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

相关标签:
6条回答
  • 2020-12-04 18:24

    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

    0 讨论(0)
  • 2020-12-04 18: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);
    

    This is how I did it.

    0 讨论(0)
  • 2020-12-04 18:25

    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.

    0 讨论(0)
  • 2020-12-04 18:35

    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

    0 讨论(0)
  • 2020-12-04 18:36

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 18:43

    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);
    }
    
    0 讨论(0)
提交回复
热议问题