Android:只需等待服务器的响应一分钟,否则将显示警报对话框
我需要显示一个对话框,如果互联网网络低,从服务器端的响应时间超过1分钟。如何完成这项任务。 我使用下面的代码。 但这不是有意的Android:只需等待服务器的响应一分钟,否则将显示警报对话框,android,response,android-alertdialog,Android,Response,Android Alertdialog,我需要显示一个对话框,如果互联网网络低,从服务器端的响应时间超过1分钟。如何完成这项任务。 我使用下面的代码。 但这不是有意的 try { HttpConnectionParams.setConnectionTimeout(hc.getParams(),60000); int timeoutSocket = 60*1000; HttpConnectionParams.setSoTimeout(hc.getParams(), timeoutSocket); } catch(Connec
try
{
HttpConnectionParams.setConnectionTimeout(hc.getParams(),60000);
int timeoutSocket = 60*1000;
HttpConnectionParams.setSoTimeout(hc.getParams(), timeoutSocket);
}
catch(ConnectTimeoutException e){
//System.out.println(e);
m_Progress.cancel();
alertDialog = new AlertDialog.Builder(AdminEbooks.this).create();
//alertDialog.setTitle("Reset...");
// System.out.println("internet not available");
alertDialog.setMessage("Low internet connectivity?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
}
听起来你需要把它放在背景线程中。我建议使用 您至少需要覆盖
onPreExecute()
、doInBackground()
和onPostExecute()
,才能完成您正在尝试的操作
onPreExecute()
和onPostExecute()
在UI线程上执行,因此可以在这些方法中显示对话框
我建议
doInBackground()
返回一个boolean
,这样onPostExecute()
就可以显示正确的对话框。听起来你需要把它放在后台线程中。我建议使用
您至少需要覆盖onPreExecute()
、doInBackground()
和onPostExecute()
,才能完成您正在尝试的操作
onPreExecute()
和onPostExecute()
在UI线程上执行,因此可以在这些方法中显示对话框
我建议
doInBackground()
返回一个boolean
,这样onPostExecute()
就可以显示正确的对话框。创建一个检查响应时间的方法
public boolean checkURL() {
boolean exist = false;
try {
URL url=new URL("http://.................");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(60000);
urlConnection.connect();
exist = true;
} catch(Exception e) {
e.printStackTrace();
exist = false;
}
return exist;
}
如果在60秒内未响应,则返回flase
现在执行条件
if(chcekURL){
} else {
alertDialog = new AlertDialog.Builder(AdminEbooks.this).create();
//alertDialog.setTitle("Reset...");
// System.out.println("internet not available");
alertDialog.setMessage("Low internet connectivity?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
}
制定检查响应时间的方法
public boolean checkURL() {
boolean exist = false;
try {
URL url=new URL("http://.................");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(60000);
urlConnection.connect();
exist = true;
} catch(Exception e) {
e.printStackTrace();
exist = false;
}
return exist;
}
如果在60秒内未响应,则返回flase
现在执行条件
if(chcekURL){
} else {
alertDialog = new AlertDialog.Builder(AdminEbooks.this).create();
//alertDialog.setTitle("Reset...");
// System.out.println("internet not available");
alertDialog.setMessage("Low internet connectivity?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.cancel();
}
});
}
我是这样做的:
public void UseHttpConnection(String url, String charset, String query) {
try {
System.setProperty("http.keepAlive", "false");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(15000 /* milliseconds */);
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset", charset);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} catch (IOException e) {
e.printStackTrace();
showError2("Check your network settings!");
} finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int status = ((HttpURLConnection) connection).getResponseCode();
Log.d("", "Status : " + status);
for (Entry<String, List<String>> header : connection
.getHeaderFields().entrySet()) {
Log.d("Headers",
"Headers : " + header.getKey() + "="
+ header.getValue());
}
InputStream response = new BufferedInputStream(connection.getInputStream());
int bytesRead = -1;
byte[] buffer = new byte[30 * 1024];
while ((bytesRead = response.read(buffer)) > 0) {
byte[] buffer2 = new byte[bytesRead];
System.arraycopy(buffer, 0, buffer2, 0, bytesRead);
handleDataFromSync(buffer2);
}
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
showError2("Check your network and server settings!");
} catch (IOException e) {
showError2("Check your network settings!");
e.printStackTrace();
}
}
public void UseHttpConnection(字符串url、字符串字符集、字符串查询){
试一试{
set属性(“http.keepAlive”、“false”);
HttpURLConnection连接=(HttpURLConnection)新URL(URL).openConnection();
connection.setDoOutput(真);
setConnectTimeout(15000/*毫秒*/);
connection.setRequestMethod(“POST”);
setRequestProperty(“Charset”,Charset);
connection.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=“+charset”);
OutputStream输出=null;
试一试{
output=connection.getOutputStream();
output.write(query.getBytes(charset));
}捕获(IOE异常){
e、 printStackTrace();
淋浴器2(“检查您的网络设置!”);
}最后{
if(输出!=null)
试一试{
output.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
int status=((HttpURLConnection)connection.getResponseCode();
Log.d(“,”状态:“+状态);
对于(条目标题:连接
.getHeaderFields().entrySet()){
Log.d(“标题”,
“标题:“+header.getKey()+”=”
+header.getValue());
}
InputStream响应=新的BufferedInputStream(connection.getInputStream());
int字节读取=-1;
字节[]缓冲区=新字节[30*1024];
而((bytesRead=response.read(buffer))>0){
byte[]buffer2=新字节[bytesRead];
数组复制(缓冲区,0,缓冲区2,0,字节读取);
handleDataFromSync(缓冲区2);
}
连接断开();
}catch(filenotfounde异常){
e、 printStackTrace();
淋浴器2(“检查您的网络和服务器设置!”);
}捕获(IOE异常){
淋浴器2(“检查您的网络设置!”);
e、 printStackTrace();
}
}
基本上,如果您的连接超时,它将抛出一个
IOException
,您需要捕获该异常并在那里创建警报对话框。至少这是我正在做的事情,它正在工作。以下是我如何做的:
public void UseHttpConnection(String url, String charset, String query) {
try {
System.setProperty("http.keepAlive", "false");
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoOutput(true);
connection.setConnectTimeout(15000 /* milliseconds */);
connection.setRequestMethod("POST");
connection.setRequestProperty("Charset", charset);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} catch (IOException e) {
e.printStackTrace();
showError2("Check your network settings!");
} finally {
if (output != null)
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int status = ((HttpURLConnection) connection).getResponseCode();
Log.d("", "Status : " + status);
for (Entry<String, List<String>> header : connection
.getHeaderFields().entrySet()) {
Log.d("Headers",
"Headers : " + header.getKey() + "="
+ header.getValue());
}
InputStream response = new BufferedInputStream(connection.getInputStream());
int bytesRead = -1;
byte[] buffer = new byte[30 * 1024];
while ((bytesRead = response.read(buffer)) > 0) {
byte[] buffer2 = new byte[bytesRead];
System.arraycopy(buffer, 0, buffer2, 0, bytesRead);
handleDataFromSync(buffer2);
}
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
showError2("Check your network and server settings!");
} catch (IOException e) {
showError2("Check your network settings!");
e.printStackTrace();
}
}
public void UseHttpConnection(字符串url、字符串字符集、字符串查询){
试一试{
set属性(“http.keepAlive”、“false”);
HttpURLConnection连接=(HttpURLConnection)新URL(URL).openConnection();
connection.setDoOutput(真);
setConnectTimeout(15000/*毫秒*/);
connection.setRequestMethod(“POST”);
setRequestProperty(“Charset”,Charset);
connection.setRequestProperty(“内容类型”,
“application/x-www-form-urlencoded;charset=“+charset”);
OutputStream输出=null;
试一试{
output=connection.getOutputStream();
output.write(query.getBytes(charset));
}捕获(IOE异常){
e、 printStackTrace();
淋浴器2(“检查您的网络设置!”);
}最后{
if(输出!=null)
试一试{
output.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
int status=((HttpURLConnection)connection.getResponseCode();
Log.d(“,”状态:“+状态);
对于(条目标题:连接
.getHeaderFields().entrySet()){
Log.d(“标题”,
“标题:“+header.getKey()+”=”
+header.getValue());
}
InputStream响应=新的BufferedInputStream(connection.getInputStream());
int字节读取=-1;
字节[]缓冲区=新字节[30*1024];
而((bytesRead=response.read(buffer))>0){
byte[]buffer2=新字节[bytesRead];
数组复制(缓冲区,0,缓冲区2,0,字节读取);
handleDataFromSync(缓冲区2);
}
连接断开();
}catch(filenotfounde异常){
e、 printStackTrace();
第2条(“Chec