Error: END_ARRAY expected, but STRING - only on some phones when using mobile Internet

advertisements

I have an android application in which I get data from web services using JSON.

I have tested the application on multiple mobile phones (samsung galaxy s2/s4, htc one/ one s, lg, sony .... ) and the problem I am getting is that when using mobile internet (on wifi everything works fine) on some devices (htc one s, Huawei Ascend p6) I get an error: expected END_ARRAY but was STRING and nothing downloads.

I can not wrap my mind why this is only happening on some devices. Did anyone experience anything similar? Please help.

Note: this is using GSON but the same problem happened when I used JSON.

JSON data:

[{"ID":"1","name":"Germany"}]

class:

public class Language {
    @SerializedName("ID")
    public int ID;

    @SerializedName("name")
    public String name;
}

Code:

try
{
    this.httpClient = new DefaultHttpClient();
    this.httpPost = new HttpPost(webserviceURL);
    this.httpPost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
    this.httpResponse = this.httpClient.execute(this.httpPost);
    this.httpEntity = this.httpResponse.getEntity();
    this.isLanguage = this.httpEntity.getContent();

    if (this.httpResponse.getStatusLine().getStatusCode() == 200)
    {
        Gson gson = new Gson();
        Reader reader = new InputStreamReader(this.isLanguage, "UTF-8");
        Language[] xy = gson.fromJson(reader, Language[].class);
        for (int i=0; i<xy.length; i++)
            Log.i("Language-GSON", "ID: " + xy[i].ID + " - Name: " + xy[i].name);
     }
}catch (Exception e) { Log.i("Language-GSON", "Exception -> " + e.getMessage()); }
finally { this.isLanguage.close(); }

Exception:

java.lang.IllegalStateException: expected END_ARRAY but was STRING at line 1 column 1


Change to

this.httpClient = new DefaultHttpClient();
this.httpPost = new HttpPost(webserviceURL);
this.httpPost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
this.httpResponse = this.httpClient.execute(this.httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String response = EntityUtils.toString(httpEntity);
Gson gson = new Gson();
Language[] xy = gson.fromJson(response, Language[].class);
for (int i=0; i<xy.length; i++)
{
     Log.i("Language-GSON", "ID: " + xy[i].ID + " - Name: " + xy[i].name);
}

I justed testing paring of json with gson locally. It works. If it is not working you will need to check the response form the server. The parsing code is fine.

Also check if the NetworkConnection is available before making a http request. Also check if the response is null or not.

It has nothing to with specific devices or mobile internet. It has something to do with the response from the server.

I am guessing your response is not a valid json. Due check the response by logging the same.