Question

Okay, I know this question has been asked before, but all the answers did not help at all. First, let me recap the problem:

I have a (PHP-generated) JSON element, the PHP code looks like this:

if (!headers_sent()) header('Content-type: application/json');
echo "[{\"test\":\"success\"}]";

No other echo, print or anything else before that, so the output really is just

[{"test":"success"}]

and nothing else. The PHP file is saved as pure UTF-8 (checked and double-checked).

Now, my Android code looks as follows:

    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://myurl");
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content, "UTF-8"), 8);
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String menuData = builder.getString();

    ArrayList<HashMap<String, String>> tempList = new ArrayList<HashMap<String, String>>();
boolean change = false;

try {
    JSONArray jsonMenu = new JSONArray(menuData);

    for (int i = 0; i < jsonMenu.length(); i++) {
        JSONObject jsonObject;
        try {
            jsonObject = jsonMenu.getJSONObject(i);

            HashMap<String, String> menuItem = new HashMap<String, String>();
            menuItem.put("test", jsonObject.getString("test"));
            tempList.add(menuItem);
            change = true;

        } catch (JSONException e) {
        }
    }

} catch (JSONException e) {
}

if(change) {
     // here comes the listview update, but never triggered anyways
}

And I always get the same error message:

04-05 21:06:31.226: W/System.err(1208): org.json.JSONException: A JSONArray text must start with '[' at character 1 of ?[{"test":"success"}]

This is actually the Logcat output saved to log.txt and parsed here, and then all of the sudden there is this question mark. All attempts to trim or format the String before parsing it to the JSONArray have failed. I have absolutely no clue anymore what else to do. In Logcat in Eclipse itself there is no question mark by the way...

Added Log.e output to the catch statements, the error output is exactly the same. Screenshot here:

Error Message in LogCat

Was it helpful?

Solution

Problem solved:

Open the PHP file in Notepad++, click on Encoding => Encode in UTF-8 without BOM.

I neglected this because it shows "ANSI as UTF-8" in the status bar which sounded just wrong (I wanted pure UTF-8 and no ANSI mix). Yeah, I'm no Linux fella, so this ANSI/UTF-8 was something I never quite got to be honest, and here it trapped me again.

If someone is interested how I found the solution (credit goes to @Perception, @Raghav-Sood, and @Reuben-Scratton here):

Converted the menuData string to a byte array and found out that the first two chars are not the opening bracket that JSONArray is looking for, but two BOM chars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top