在android中设置连接超时

时间:2011-12-21 10:47:20

标签: android connection

我正在使用android中的一些web服务,我希望如果在1分钟内没有响应,它应该显示一些互联网不可用的警报对话框。 我正在使用连接超时,如下面的代码所示:

try
{
    HttpPost httppost =new HttpPost(Constants.getHostString() + "/apps_user_ebooks_preview.jsp");
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 60000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 60000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);       
    DefaultHttpClient httpClient =new DefaultHttpClient(httpParameters);
    BasicHttpResponse httpResponse =(BasicHttpResponse) httpClient.execute(httppost);
    httpClient.setParams(httpParameters);
    HttpEntity entity = httpResponse.getEntity();
    ResponseHandler<String> res =new BasicResponseHandler();
    List<NameValuePair> NVP =new ArrayList<NameValuePair>();
    NVP.add(new BasicNameValuePair("postString", j.toString()));
    httppost.setEntity(new UrlEncodedFormEntity(NVP));
    response = httpClient.execute(httppost, res);
}
catch(ConnectTimeoutException e){
    e.printStackTrace();
    System.out.println("here i am ");
    handler.sendEmptyMessage(CONNECTION_TIMEOUT);
    cnct=1;                                             
}
case CONNECTION_TIMEOUT:
    if (!isFinishing())
    {
        // m_ProgressDialog.cancel();
        lDialog = new Dialog(AdminEbooks.this, android.R.style.Theme_Translucent_NoTitleBar);
        lDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        lDialog.setContentView(R.layout.r_okdialogview);
        ((TextView) lDialog.findViewById(R.id.dialog_title)).setVisibility(View.GONE);
        ((TextView) lDialog.findViewById(R.id.dialog_message)).setText("Internet Unavailable.");
        ((Button) lDialog.findViewById(R.id.ok)).setText("Ok");
        ((Button) lDialog.findViewById(R.id.ok)).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                lDialog.dismiss();
            }
         });
        lDialog.show();
    }
break;

但它不起作用。 谁能帮助我呢?

0 个答案:

没有答案