Displaying More string on 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 maximu开发者_Go百科m string length that could be displayed by logcat.
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);
}
}
if you are not using eclipse, or you are but @Nanne answer doesn't work for you I can only think in two alternatives:
- 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
- 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.
精彩评论