保持连接打开并读取数据,直到强制关闭

Nit*_*ish 3 android httprequest

当我的活动加载时,我正在连接到Web服务.当我从服务中得到响应时,我再次呼叫服务等等.

@Override
protected void onCreate(Bundle savedInstanceState) {  
….  
callWebMethod();
}  

// Called on getting response  
@Override
public void run(String value) {  
….  
callWebMethod();  
}  
Run Code Online (Sandbox Code Playgroud)

这就是我连接服务的方式

HttpGet request = new HttpGet(url + combinedParams);  
HttpClient client = new DefaultHttpClient(httpParameters);

    HttpResponse httpResponse;

        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);
            response = StringUtils.remove(response, "\n");
            response = StringUtils.remove(response, '"');
        }  
Run Code Online (Sandbox Code Playgroud)

我是否有可能在开始时只连接一次服务,然后连接保持打开状态,应用程序继续从服务中读取数据,直到强制关闭连接.
如果需要更多代码,请告诉我.

更新:然后我尝试使用ClientConnectionManager但仍然连接一次又一次初始化.虽然它正在获取数据.我想要的是连接保持打开状态,并继续从服务中读取数据.

HttpParams httpParameters = new BasicHttpParams();

    SharedPreferences preferences = context.getSharedPreferences(
            "MyPreferences", Context.MODE_PRIVATE);

    int timeoutConnection = Integer.parseInt(preferences.getString(
            "timeout", "60")) * 1000;
    HttpConnectionParams.setConnectionTimeout(httpParameters,
            timeoutConnection);

    HttpConnectionParams.setSoTimeout(httpParameters, 2000);
    System.setProperty("http.keepAlive", "true");
    HttpClient client = new DefaultHttpClient(httpParameters);

    ClientConnectionManager mgr = client.getConnectionManager();
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(
            client.getParams(), mgr.getSchemeRegistry()),
            client.getParams());
    while (true) {

        HttpResponse httpResponse;

        try {
            httpResponse = client.execute(request);
            responseCode = httpResponse.getStatusLine().getStatusCode();
            message = httpResponse.getStatusLine().getReasonPhrase();

            HttpEntity entity = httpResponse.getEntity();

            if (entity != null) {

                InputStream instream = entity.getContent();
                response = convertStreamToString(instream);
                response = StringUtils.remove(response, "\n");
                response = StringUtils.remove(response, '"');
                ((Activity) context).runOnUiThread(new Runnable() {
                    public void run() {
                        callback.run(response);  // This calls activity callback function.
                    }
                });

                // Closing the input stream will trigger connection release
                // instream.close();
            }

        } catch (ConnectTimeoutException e) {
         ….  
         }
Run Code Online (Sandbox Code Playgroud)

Jst*_*wll 13

听起来你真正需要的是套接字连接(见这里).套接字将保持连接状态,允许您使用套接字服务器来回传输数据,直到完成为止.