我必须处理以下特殊情况:
在上述情况发生时,我可以获得广播或回调吗?
答案 0 :(得分:0)
最简单易行的方法是:ping到这里的任何服务器。我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());
}
}