Handle response properly when used HttpClient

2019-05-30 13:56发布

问题:

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 ?