android httpconnection超时无法正常工作

时间:2014-08-27 18:07:56

标签: android timeout httpurlconnection

当我打开互联网(数据)dialog.dismiss();工作正常,对话消失了。但如果我关闭互联网对话框不会消失。我等了1分钟或更长时间。顺便说一句,这是一个后台线程。 我不需要检查互联网。只是想超时并需要运行dialog.dismiss();连接完成或超时后。 我的代码是

connection = (HttpURLConnection) (new URL("http://anything.com/a/?enask="+textsearched )).openConnection();
 connection.setRequestMethod( "GET" );
 connection.setConnectTimeout(5000);
 connection.setReadTimeout(10000);
 connection.setRequestProperty("Connection", "Keep-Alive");
  connection.setDoInput( true );
 connection.setDoOutput( true );
 connection.connect();
 InputStream is = null;
 is = connection.getInputStream();
 dialog.dismiss();

1 个答案:

答案 0 :(得分:0)

您可以先查看是否在线。

    public boolean isOnline() 
        {
        ConnectivityManager cm =(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) 
            {return true;}
        return false;
        }

否则否则