我必须处理以下特殊情况:
当发生上述情况时,我是否可以得到广播或回调?
发布于 2014-09-15 02:45:34
最简单、最简单的方法是:像这里这样对任何服务器进行ping操作。我点击谷歌,检查回复是否为空;) .当您对连接性有疑问时,这个函数在android活动中调用。
public void GET(){
String result = "";
HttpResponse response;
HttpEntity entity;
try {
HttpPost httppost = new HttpPost("http://google.com");
DefaultHttpClient httpclient = getHttpClientImpl();
response = httpclient.execute(httppost);
entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"), 8 * 1024);
while ((line = reader.readLine()) != null) {
sb.append(line);// .append("\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
}
result = sb.toString();
if (responseXML.equals("")){
// Internet connectivity is lost.
}else {
// Internet connectivity still here xnjoy.
}
}
entity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
}
https://stackoverflow.com/questions/25845896
复制相似问题