Logcat output shows question marks in Log.d/i output from fetchItems


#1

The Logcat output shows only question marks.
Is there a way to format the output to give readable output ?

01-15 12:08:50.117 3624-19337/…Fetchr: Received JSON: ��������������������������������������������������
01-15 12:08:50.272 3624-19337/…Fetchr: org.json.JSONException: Value ��������������������������������������������������

Probably it goes wrong in getUrlBytes, it pulls in zeros resulting in question marks in logcat output …

public byte[] getUrlBytes(String urlSpec) throws IOException {

    URL url = new URL(urlSpec);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream inputStream = connection.getInputStream();

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException(connection.getResponseMessage() + ": with " + urlSpec);
        }

        int bytesRead = 0;
        byte[] buffer = new byte[1024];

        while ((bytesRead = inputStream.read()) > 0) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
        return outputStream.toByteArray();
    } finally {
        connection.disconnect();
    }
}

#2

Ouch …
while ((bytesRead = inputStream.read()) > 0) {
must be:
while ((bytesRead = inputStream.read(buffer)) > 0) {


#3

Wow thank you so much. I had the same problem and spent a whole day scratching my head.