Android url不可访问或internet不可用时的网络连接异常处理

Android url不可访问或internet不可用时的网络连接异常处理,android,Android,我的应用程序崩溃,原因如下: 如果互联网连接不可用,则表示设备未连接到WiFi或WiFi连接受限 如果无法访问该网站 如何处理这些情况?要检查连接可用性,在调用以下方法之前,请使用ConnectionManager检查连接可用性。如果不可用,则显示使用错误 如果连接可用且无法访问URL/服务器,则以下解决方案将帮助您 对于使用post web服务,我们使用以下代码: 如果响应为空,则可能是服务器问题,我们会相应地处理它 try{ String out_url="http://algoti

我的应用程序崩溃,原因如下:

  • 如果互联网连接不可用,则表示设备未连接到WiFi或WiFi连接受限
  • 如果无法访问该网站

如何处理这些情况?

要检查连接可用性,在调用以下方法之前,请使用ConnectionManager检查连接可用性。如果不可用,则显示使用错误

如果连接可用且无法访问URL/服务器,则以下解决方案将帮助您

对于使用post web服务,我们使用以下代码: 如果响应为空,则可能是服务器问题,我们会相应地处理它

try{
  String out_url="http://algotips.com/";
  URL urlObject=new URL(out_url);
  conn = (HttpURLConnection) urlObject.openConnection();
  conn.setReadTimeout(100000);//milliseconds
  conn.setConnectTimeout(150000);//milliseconds
  conn.setRequestMethod("GET");
  conn.setDoInput(true);
}
catch(Exception e){ 
   /*Exception*/ 
}
私有HttpResponse getWebServiceResponse(字符串URL,ArrayList参数)
{
HttpResponse HttpResponse=null;
尝试
{
HttpParams httpParameters=新的BasicHttpParams();
//以毫秒为单位设置超时,直到建立连接。
//默认值为零,表示不使用超时。
int timeoutConnection=20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=20000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient(httpParameters);
httpClient.getCredentialsProvider().setCredentials(
AuthScope.ANY,新用户名密码凭据(appProps.getProperty(“用户名”)、appProps.getProperty(“密码”);
HttpPost HttpPost=新的HttpPost(URL);
尝试
{
setEntity(新的UrlEncodedFormEntity(参数));
}捕获(不支持的编码异常e){
}
httpResponse=httpClient.execute(httpPost);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}       
返回httpResponse;
}
对于Get请求,相应地进行更改(包括超时和套接字超时,如上例所示):

//用于调用GET方法
公共静态void callWebServiceGET(字符串URL、字符串用户名、字符串密码、ArrayList参数)
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet-HttpGet;
字符串querystring=URLEncodedUtils.format(参数“utf-8”);
URL=URL+“?”+查询字符串;
log.info(“通过GET请求使用的URL为-->”+URL);
httpget=新的httpget(URL);
HttpResponse HttpResponse=null;
尝试
{
httpget.addHeader(BasicScheme.authenticate(
新用户名密码凭据(用户名、密码),
“UTF-8”,假);
httpResponse=httpclient.execute(httpget);
HttpEntity entity=httpResponse.getEntity();
String respstring=EntityUtils.toString(实体);
如果(实体!=null)
{
entity.consumercontent();
}
}捕获(例外e){
log.error(“捕获的异常::”+e);
e、 printStackTrace();
}
}//方法callWebServiceGET的结尾

在尝试连接到internet之前,您必须先检查internet

// For calling GET method
public static void callWebServiceGET (String URL, String userName, String password, ArrayList<NameValuePair> params)
{
    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget;
    String querystring = URLEncodedUtils.format(params, "utf-8");
    URL = URL + "?" + querystring;
    log.info("URL to be consumed via GET request is --> " + URL);
    httpget = new HttpGet(URL);
    HttpResponse httpResponse = null; 
    try 
    {
        httpget.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials(userName, password),
                "UTF-8", false));

        httpResponse = httpclient.execute(httpget);

        HttpEntity entity = httpResponse.getEntity();

        String respstring = EntityUtils.toString(entity);


        if (entity != null)
        {
            entity.consumeContent();
        }

    } catch (Exception e) {
        log.error("Exception caught :: " +e);
        e.printStackTrace();
    }

} // End of method callWebServiceGET

您是否在manifest.xml上写入了正确的权限?当你的应用程序崩溃时,你会遇到哪个错误。。??
// For calling GET method
public static void callWebServiceGET (String URL, String userName, String password, ArrayList<NameValuePair> params)
{
    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget;
    String querystring = URLEncodedUtils.format(params, "utf-8");
    URL = URL + "?" + querystring;
    log.info("URL to be consumed via GET request is --> " + URL);
    httpget = new HttpGet(URL);
    HttpResponse httpResponse = null; 
    try 
    {
        httpget.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials(userName, password),
                "UTF-8", false));

        httpResponse = httpclient.execute(httpget);

        HttpEntity entity = httpResponse.getEntity();

        String respstring = EntityUtils.toString(entity);


        if (entity != null)
        {
            entity.consumeContent();
        }

    } catch (Exception e) {
        log.error("Exception caught :: " +e);
        e.printStackTrace();
    }

} // End of method callWebServiceGET
private boolean isConnected(Context ctx){
ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Activity.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isConnected()) 
                return true;
            else
                return false;   
    }