user3906863
user3906863

Reputation: 53

JSON response is getting cut off, Android

I've seen multiple posts about this topic, but none of them seem to be the solution to my problem.

The problem is that the JSON response from the server is getting cut off and therefore I'm getting a JSONException when trying to get the response into a JSONArray.

    json = new JSONArray(EntityUtils.toString(response.getEntity()));

Here is the whole code:

    private class AsyncFetchForms extends AsyncTask<String, Void, JSONArray> {

    private HttpClient mClient = new DefaultHttpClient();
    private AsyncTaskCompleteListener<JSONArray> listener;
    private String serverUrl;
    private String credentials;

    private ProgressDialog progressDialog;
    private HttpGet httpGet;
    private String response;
    private BasicResponseHandler responseHandler;
    private boolean showDialog;
    private JSONArray json;



    public AsyncFetchForms(String url, String message, AsyncTaskCompleteListener<JSONArray> listener, boolean showDialog)
    {
        serverUrl = Utils.getServerUrl(context) + url;
        credentials = Utils.getUserCredentials(context);
        this.listener = listener;
        this.showDialog = showDialog;

        httpGet = new HttpGet(serverUrl);
        httpGet.setHeader("Authorization", "Basic " + credentials);
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Accept-Encoding", "gzip");
        httpGet.setHeader("Connection", "keep-alive");

        responseHandler = new BasicResponseHandler();


        if(showDialog)
        {
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage(message);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);          
            progressDialog.show();
        }
    }


    @Override
    protected JSONArray doInBackground(String... params) {

        try {
            HttpResponse response = mClient.execute(httpGet);


            if (response.getStatusLine().getStatusCode() == 200) {

                json = new JSONArray(EntityUtils.toString(response.getEntity()));

                return json;
            }
        } catch (ClientProtocolException e) {
           e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        System.out.println(result.toString());

    }
}

Can anyone see the problem?

Upvotes: 5

Views: 3707

Answers (4)

Lie Ryan
Lie Ryan

Reputation: 64855

If you (or your team) implement the server side yourself, first thing I'd check is if the server is returning the correct HTTP response. In particular, if you transfer the data by HTTP, you need to have correct Content-Length or otherwise your data will be cut off. Also, Content-Length must be the length of data after any Transfer Encodings are applied, in other words, after the length of the data after being gzipped. Alternatively, use chunked transfer.

Second, make sure that your server is generating valid JSON. Maybe you missed a closing parentheses or so. Maybe you need to parse JSON Object rather JSON Array.

Also, if you receive exceptions, please always post the the entire traceback.

Upvotes: 1

poulpi
poulpi

Reputation: 101

This answer is completely out of the subject but :

What are you trying do here ? Do you know that there are libraries which are doing all this boring job for you ?

When I talk about boring job, I'm talking about managing all the background running stuff (like AsyncTask), JSON decoding and HTTP response. I know that it's sometimes a pain in the a** (I've been there) but now I've choose to not worry anymore and use a dedicated library : http://square.github.io/retrofit/

This little baby will contact the Webservice of your choice, download the JSON and put it into a custom java class with all the attributes you want to deal with.

If you plug it with something like ORMLite, it can even save your JSON response object into a SQLite DB that you can access in the same way (it "populates" a java class object by setting all the attributes for you).

Personally I can't imagine myself doing all this stuff by hand anymore, it's just trouble without the benefits =)

Upvotes: 0

Ahmed Daif
Ahmed Daif

Reputation: 379

First of all, try to log the EntityUtils.toString(response.getEntity()) response and make sure that it starts with "[" not "{" ie. it's a jsonArray not jsonObject.

Then try to open the url in your browser ,if avilable, and make sure that there are no encoding issues.

Finally, if the problem is still exists please send us the error log output.

Upvotes: 0

Illegal Argument
Illegal Argument

Reputation: 10338

Logcat can only show about 4000 characters. So you will have to implement a recursive function to see the entire log. Use below function to see the entire log:

public static void longInfo(String str) {
    if (str.length() > 4000) {
        Log.d("", str.substring(0, 4000));
        longInfo(str.substring(4000));
    } else
        Log.d("", str);
}

Upvotes: 5

Related Questions