Handle response properly when used HttpClient
Asked Answered
L

0

1

I am developing a android app where I am using Zend framework to build APIs. I am calling API using this code. This is code in IntentService class.

    HttpClient client = new DefaultHttpClient();

        // Set timeout values
    client.getParams().setIntParameter(
                CoreConnectionPNames.CONNECTION_TIMEOUT,
                CONNECTION_TIMEOUT * 1000);
    client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                SOCKET_TIMEOUT * 1000);try {

            HttpUriRequest httpRequest = null;
            HttpResponse response = null;

            // Set HttpUriRequest based on type of HTTP method
            if (method.equals("GET")) {

                HttpGet request = new HttpGet(url);
                httpRequest = request;

            } 

            // Get response
            response = client.execute(httpRequest);
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));

            // Read the response
            String responseString = readResponseContent(rd);

            Log.e(TAG, "length of response is " + responseString.length());

            Log.e(TAG, "response :" + responseString);
            // If status code 200 then send response

        }catch (Exception e) {
            MyLog.e(TAG, "Exception while connecting to URL : " + url);
            e.printStackTrace();
            sendUnknownError();

        } 

Now the problem is I have created one API which response is very long. But After getting this I am sending broadcast to where it is called. It returns response in chunks So when it receives response first time but actually response is not yet completed. So that's causing some issues later on. So How can I wait for full response before calling broadcast method.

So I need to wait for it to get full response. How I can do this ?

Limemann answered 18/4, 2014 at 7:8 Comment(3)
you need to increase the Time for Connection time out and socket time value from 1 sec to least 15 sec then tryJaunita
But it is already 20 seconds. You see CONNECTION_TIMEOUT and SOCKET_TIMEOUT. Both variables value is 20. So it will be 20 seconds.Limemann
why it is voted for close. Please specify a reason.Limemann

© 2022 - 2024 — McMap. All rights reserved.