使用HttpClient时正确处理响应

时间:2014-04-18 07:08:59

标签: android httpclient bufferedreader androidhttpclient chunks

我正在开发一个Android应用程序,我使用Zend框架来构建API。我正在使用此代码调用API。这是IntentService类中的代码。

    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();

        } 

现在的问题是我创建了一个响应非常长的API。但在得到这个之后,我正在向所谓的地方发送广播。它以块的形式返回响应因此当它第一次收到响应但实际上响应尚未完成时。所以这会在以后引起一些问题。那么我怎么能在调用广播方法之前等待完整的响应。

所以我需要等待它得到完整的回复。我怎么能这样做?

0 个答案:

没有答案