ksoap2-android访问webservice,当网络有代理的情况(cmwap)

个人主页已迁移到: http://www.blog4app.com        

ksoap2-android是在android平台,访问webservice的外部jar包。

 

        项目遇到的bug如下:当手机使用wifi或者net访问webservice的时候一切正常,但是当手机网络为wap的情况下,就连接失败。

 

 

        解决办法:ksoap2-android最新版本是2.5.4。在2.5.2之前源码都是不支持代理访问的,在2.5.4之后,源码增加了对网络有代理的支持。

 

        下面把主要代码贴出来:

 

 

//判断当前网络是否是net,true为net,false为wap public static boolean isGprsNet(){ String proxyHost = android.net.Proxy.getDefaultHost(); return proxyHost==null; } //根据网络类型返回相应的HttpTransportSE webservice用 public static HttpTransportSE getHttpTransportSE(Context context){ HttpTransportSE ht; if(!HttpUtil.isGprsNet()){ java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort())); ht=new HttpTransportSE(p,context.getString(R.string.wsurl)); }else{ ht = new HttpTransportSE(context.getString(R.string.wsurl)); } return ht; } /* * 登录接口 1成功 0失败 */ public String login(Context context,String phoneNumber,String pwd){ SoapObject request = new SoapObject(context.getString(R.string.namespace), "login"); request.addProperty("usrPhone", phoneNumber); request.addProperty("passWord", pwd); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11 ); envelope.bodyOut=request; HttpTransportSE ht = HttpUtil.getHttpTransportSE(context); try { ht.call(null, envelope); } catch (Exception e) {} SoapObject so = null; so = (SoapObject) envelope.bodyIn; String result= so.getProperty("return").toString(); return result; }  

 

 

      androi下net和wap自适应代码:

 

 

private HttpURLConnection getURLConnection(String url) throws Exception { String proxyHost = android.net.Proxy.getDefaultHost(); if (proxyHost != null) { java.net.Proxy p = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(android.net.Proxy.getDefaultHost(), android.net.Proxy.getDefaultPort())); return (HttpURLConnection) new URL(url).openConnection(p); } else { return (HttpURLConnection) new URL(url).openConnection(); } }  

 

 

你可能感兴趣的:(exception,网络,String,webservice,url)