java post 超时时间_在异步post方法中设置连接超时的方法

博主使用asynchronous post method向服务器发送数据,若服务器关闭或无响应,应用程序会强制关闭。博主询问如何为post请求设置超时时间,还给出了异步发送请求的代码示例,并编辑询问如何通过警报通知用户连接超时及设置条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在使用 asynchronous post method 将一些数据发布到服务器 . 该帖子工作正常,但如果服务器关闭或无响应,那么我将在应用程序中关闭一个强制关闭 .

How should I implement a timeout to the post request?

这是 class which is asynchronously posting to a particular url :

//===================================================================================================================================

//sending EmailAddress and Password to server

//===================================================================================================================================

private class MyAsyncTask extends AsyncTask{

@Override

protected Double doInBackground(String... params) {

// TODO Auto-generated method stub

postData(params[0],params[1]);

return null;

}

protected void onPostExecute(Double result){

if(responseBody.contains("TRUE"))

{

String raw=responseBody;

raw = raw.substring(0, raw.lastIndexOf("

raw = raw.substring(raw.lastIndexOf(">") + 1, raw.length());

String [] contents = raw.split(",");

//extracting user name and user id from response

String user_name=contents[1];

String student_code=contents[2];

//save user name and user id in preference

saveInPreference("user_name",user_name);

saveInPreference("student_code",student_code);

//login is successful, going to next activity

Intent intent = new Intent(LoginActivity.this, TakeTestActivity.class);

//hiding progress bar

progress.dismiss();

finish();

LoginActivity.this.startActivity(intent);

}

else

{

//hiding progress bar

progress.dismiss();

create_alert("Attention!", "Please provide valid userid and password");

}

}

protected void onProgressUpdate(Integer... progress){

}

public void postData(String emailId,String passwrd) {

**//EDIT START**

HttpParams httpParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams, 10000);

HttpConnectionParams.setSoTimeout(httpParams, 10000);

HttpClient httpclient = new DefaultHttpClient(httpParams);

**//EDIT END**

// Create a new HttpClient and Post Header

//HttpClient httpclient = new DefaultHttpClient();

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);

final String url_first = preferences.getString("URLFirstPart","");

HttpPost httppost = new HttpPost(url_first+"ValidateLogin");

try {

// Data that I am sending

List nameValuePairs = new ArrayList();

nameValuePairs.add(new BasicNameValuePair("EmailId", emailId));

nameValuePairs.add(new BasicNameValuePair("Password", passwrd));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

**//EDIT START**

try

{

// Execute HTTP Post Request

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

responseBody = EntityUtils.toString(response.getEntity());

}

catch (SocketTimeoutException ex)

{

// Do something specific for SocketTimeoutException.

}

**//EDIT END**

//Log.d("result", responseBody);

}

catch (Throwable t ) {

}

}

}

//===================================================================================================================================

//END sending EmailAddress and Password to server

//===================================================================================================================================

我就是这样 calling the class to execute the post request :

//sending request for login

new MyAsyncTask().execute(txtUsername.getText().toString(),txtPassword.getText().toString());

What should I do to implement a connection timeout after a particular time if the server does not respond or is not available?

Edited:

How do I notify the user using an alert that the connection has timed out? Where should I put the alert and during which condition?

提前致谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值