在android中设置连接超时
我正在android中使用一些web服务,我希望如果响应在1分钟内没有出现,它应该显示一些警告对话框,说明internet不可用。 我正在使用连接超时,如下代码所示:在android中设置连接超时,android,connection,Android,Connection,我正在android中使用一些web服务,我希望如果响应在1分钟内没有出现,它应该显示一些警告对话框,说明internet不可用。 我正在使用连接超时,如下代码所示: try { HttpPost httppost =new HttpPost(Constants.getHostString() + "/apps_user_ebooks_preview.jsp"); HttpParams httpParameters = new BasicHttpParams(); int
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;
试试看
{
HttpPost-HttpPost=newhttppost(Constants.getHostString()+“/apps\u user\u ebooks\u preview.jsp”);
HttpParams httpParameters=新的BasicHttpParams();
int timeoutConnection=60000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
int timeoutSocket=60000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
DefaultHttpClient httpClient=新的DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse=(BasicHttpResponse)httpClient.execute(httppost);
httpClient.setParams(httpParameters);
HttpEntity entity=httpResponse.getEntity();
ResponseHandler res=新的BasicResponseHandler();
List NVP=new ArrayList();
添加(新的BasicNameValuePair(“postString”,j.toString());
setEntity(新的UrlEncodedFormEntity(NVP));
response=httpClient.execute(httppost,res);
}
捕获(ConnectTimeoutException e){
e、 printStackTrace();
System.out.println(“我在这里”);
handler.sendEmptyMessage(连接超时);
cnct=1;
}
案例连接超时:
如果(!isFinishing())
{
//m_ProgressDialog.cancel();
lDialog=新建对话框(AdminEbooks.this、android.R.style.Theme\u transparent\u NoTitleBar);
lDialog.requestWindowFeature(窗口功能\u编号\u标题);
lDialog.setContentView(R.layout.R\u对话框视图);
((TextView)lDialog.findViewById(R.id.dialog_title)).setVisibility(View.GONE);
((TextView)lDialog.findviewbyd(R.id.dialog_message)).setText(“互联网不可用”);
((按钮)lDialog.findViewById(R.id.ok)).setText(“ok”);
((按钮)lDialog.findViewById(R.id.ok)).setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
二、辞退;
}
});
lDialog.show();
}
打破
但它不起作用。
有人能帮我解决这个问题吗?你有没有让它工作过?我知道有点晚了…你有没有试过?我知道有点晚了。。。