在我的应用程序中,我发送了多个http请求。因为,网络可能会在任何时候中断。那么,在发送每个请求之前,我应该使用android ConnectionManager检查网络状态吗?我觉得有其他更好的方法来处理这个问题(而不是每次在每个片段上发送多个请求时都使用android连接管理器服务)。
发布于 2014-01-18 15:18:47
如果您想避免使用ConnectionManager,可以使用try-catch块,并在尝试建立连接时检查异常(假设您正在使用java.net.*库进行this question.请求)。例如,在该问题中使用了类似以下内容:
URL url = ...
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setReadTimeout(15000);
try {
InputStream in = new BufferedInputStream(c.getInputStream());
httpResult = readStream(in);
} catch (IOException e) {
// Here is where an exception would be thrown if there is no internet.
// Would likely actually be an UnknownHostException
Log.e(TAG, "Error: ", e);
} finally {
c.disconnect();
}https://stackoverflow.com/questions/21201216
复制相似问题