我的 android 应用程序通过 http 连接连接到我的服务器。服务器将在 10 秒后关闭连接。但是在客户端(android)我想在 5 秒内关闭连接无论我是否从服务器接收数据我的 android 应用程序都必须关闭连接。 当我尝试 setsotimeout(5000) 时发生了两件事 1.Android 应用程序每 5000 毫秒一次又一次地发送请求。 2.在服务器关闭的情况下也能正常工作。 给我一些好的逻辑建议
protected String doInBackground(Object[] params)
{
try
{
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("carddata", "this " + "is " + "normal" + "transaction"));
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/MyServletProject/DoubleMeServlet");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
httpPost.setParams(httpParameters);
HttpResponse response = httpClient.execute(httpPost);
Log.d("Http Post Response:", response.toString());
HttpEntity entity = response.getEntity();
dsds = EntityUtils.toString(entity);
} catch (Exception e)
{
if (e.toString().equals("java.net.SocketTimeoutException"))
{
dsds=e.toString();
}
最佳答案
如果我是你,我会在 by http 客户端和我的应用程序之间创建一个层。每次发出请求时,它都会重新启动一个 5000 毫秒的计时器来终止连接。
关于Android服务器http连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501370/