简体   繁体   English

Android中的Java JSON和字符串

[英]java JSON and string in Android

I am querying a database through php in android. 我正在通过android中的php查询数据库。 Here is my relevant code 这是我的相关代码

String result = null;
InputStream is = null;
StringBuilder sb = null;

is = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
   sb.append(line + "\n");
}
is.close();
result = sb.toString();    
JSONArray jArray = new JSONArray(result);

Here is the link and returned data I am trying to parse. 这是我尝试解析的链接和返回的数据。

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522%2C151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyCX8PnG_dSJvCMmQPN1Zjbpi_RcokyiOss https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522%2C151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyCX8PnG_dSJvCMmQPN1Zjbpi_RcokyiOsss

Everything went fine till the last statement. 一切顺利,直到最后一次声明。 When I try to print out the length of result, it's of correct length. 当我尝试打印结果的长度时,它是正确的长度。 However, if I try to print out result using 但是,如果我尝试使用打印输出结果

Log.e ("ERROR", result);

and adb logcat to look at the output,it's truncated. 和adb logcat查看输出,它被截断了。 So if the truncated string result was passed into JSONArray, it'll definitely crash. 因此,如果将截断的字符串结果传递到JSONArray中,则肯定会崩溃。 It was always truncated after "Pyrmont Bay Wh", which is about 4048 characters down the returned value. 它总是在“ Pyrmont Bay Wh”之后被截断,它比返回值低4048个字符。 So guess my question is why the variable "result" was truncated. 因此,请猜我的问题是为什么变量“结果”被截断了。 thanks 谢谢

Logcat truncates long strings. Logcat会截断长字符串。

$ adb logcat -g
/dev/log/main: ring buffer is 64Kb (63Kb consumed), max entry is 5120b, max payload is 4076b

One thing you can do is split the string into printable chunks as suggested in this SO post : 您可以做的一件事是按照此SO post的建议将字符串拆分为可打印的块:

    int maxLogSize = 1000;
    for(int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
        int start = i * maxLogSize;
        int end = (i+1) * maxLogSize;
        end = end > veryLongString.length() ? veryLongString.length() : end;
        Log.v(TAG, veryLongString.substring(start, end));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM