2014-01-18 46 views
0

在我的应用程序中,我发送多个http请求。 因为,网络可能随时停机。那么,在发送每个请求之前,我应该使用android ConnectionManager来检查网络状态?我觉得还有其他更好的方法可以处理这个问题(每次在每个片段上发送多个请求时,不要使用android连接管理器服务)处理会话,网络状态和http请求

回答

2

请看this question.如果您想避免使用ConnectionManager,您可以使用try-catch块并在尝试建立连接时检查异常(前提是您使用java.net.*库来执行HTTP请求)。例如,在这个问题中使用了类似的东西:

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(); 
}