大佬教程收集整理的这篇文章主要介绍了android – 保持连接打开并读取数据,直到强制关闭,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@Override protected void onCreate(Bundle savedInstanceStatE) { …. callWebMethod(); } // Called on getTing response @Override public void run(String value) { …. callWebMethod(); }
这就是我连接服务的方式
httpGet @R_801_10613@est = new httpGet(url + combinedParams); httpClient client = new DefaulthttpClient(httpParameters); httpResponse httpResponse; httpResponse = client.execute(@R_801_10613@est); 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,'"'); }
我是否有可能在开始时只连接一次服务,然后连接保持打开状态,应用程序继续从服务中读取数据,直到强制关闭连接.
如果需要更多代码,请告诉我.
更新:然后我尝试使用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.getscheR_54_11845@eRegistry()),client.getParams()); while (true) { httpResponse httpResponse; try { httpResponse = client.execute(@R_801_10613@est); 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) { …. }
以上是大佬教程为你收集整理的android – 保持连接打开并读取数据,直到强制关闭全部内容,希望文章能够帮你解决android – 保持连接打开并读取数据,直到强制关闭所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。