简体   繁体   中英

java JSON and string in Android

I am querying a database through php in android. 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

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. So if the truncated string result was passed into JSONArray, it'll definitely crash. It was always truncated after "Pyrmont Bay Wh", which is about 4048 characters down the returned value. So guess my question is why the variable "result" was truncated. thanks

Logcat truncates long strings.

$ 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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