Handle response properly when used HttpClient

孤街醉人 提交于 2019-12-08 07:33:23

问题


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 ?

来源:https://stackoverflow.com/questions/23149187/handle-response-properly-when-used-httpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!