我目前正在开发一个使用“JSON”作为服务器响应的 android 应用程序。通常我处理 JSON 响应。但是现在我遇到了logcat的问题,如果JSON响应字符串很长,超过x个字符(我不知道logcat可以显示的最大字符串到底是多少),一些JSON字符串丢失了.
虽然它仍然可以给我输出,但我需要从服务器传输的 JSON 字符串的信息。
是否有可能在 logcat 上显示更多字符串?就像增加缓冲区或任何我可以用来增加 logcat 可以显示的最大字符串长度的参数一样。
丑陋但它可以完成工作:
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);
我就是这样做的。
我们的一些 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);
}
}
如果您没有使用 eclipse,或者您使用了,但 @Nanne 答案对您不起作用,我只能想到两种选择:
编辑:另一种可能性:将JSON像日志一样写入SD卡中的文件,然后在要检查响应时检索文件
为什么不从命令行使用 logcat?
我怀疑它是否会是你所期望的,但为什么不试一试呢?
发出命令
./adb -e logcat
从具有 adb 的目录中。这是用于模拟器的。替换-e
为-d
设备
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.