90%都连不上,或者100%连不上,不管是本机还是手机都是如此:
经查:OutputStream outStream=con.getOutputStream();处出了问题。这是为什么?
代码都是从别人完成的项目里copy出来的。别人的项目都能运行,我的为什么不能?
开发环境:vs2012 windows server 2008 r2 服务器:iis 防火墙什么的都关闭了。
还有在iis上配置webservcie 有什么注意的地方?
出问题的代码如下:
public class HttpConnSoap {
public ArrayList<String> GetWebServre(String methodName,ArrayList<String> Parameters,ArrayList<String>ParValues)
{
ArrayList<String> Values=new ArrayList<String>();
String ServerUrl="http://192.168.1.101/Service1.asmx";
 //String soapAction="http://tempuri.org/LongUserId1";
String soapAction="http://tempuri.org/"+methodName;
 String data="";
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"  
            + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"  
            +"<soap:Body />";
         String tps,vps,ts;
            String mreakString="";
            mreakString="<"+methodName+" xmlns=\"http://tempuri.org/\">";
            for ( int i = 0; i < Parameters.size(); i++) {
     tps=Parameters.get(i).toString();
     //设置该方法的参数为.net webService中的参数名称
             vps=ParValues.get(i).toString();
     ts="<"+tps+">"+vps+"</"+tps+">";
     mreakString=mreakString+ts;
     }
            mreakString=mreakString+"</"+methodName+">";
            /*
            +"<HelloWorld xmlns=\"http://tempuri.org/\">"
            +"<x>string11661</x>"
            +"<SF1>string111</SF1>"
            + "</HelloWorld>"
            */
            String soap2="</soap:Envelope>"; 
            String requestData=soap+mreakString+soap2;
    System.out.println(requestData); try{
URL url =new URL(ServerUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
System.out.println("1");
byte[] bytes=requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(8000);// 设置超时时间
System.out.println("2");
con.setRequestMethod("POST");
System.out.println("3");
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
System.out.println("4");
con.setRequestProperty("SOAPAction",soapAction);
System.out.println("5");
con.setRequestProperty("Content-Length",""+bytes.length);
System.out.println("6");
OutputStream outStream=con.getOutputStream();//出问题处
System.out.println("7");
outStream.write(bytes);
System.out.println("8");
outStream.flush();
outStream.close();
InputStream inStream=con.getInputStream(); //data=parser(inStream);
//System.out.print("11");
Values= inputStreamtovaluelist(inStream,methodName);
//System.out.println(Values.size());
return Values;

}
catch(Exception e)
{
System.out.print("2221");
return null;
}
}
public   ArrayList<String>   inputStreamtovaluelist  (InputStream   in,String MonthsName)   throws   IOException   { 
        StringBuffer   out   =   new   StringBuffer();
        String s1="";
        byte[]   b   =   new   byte[4096]; 
        ArrayList<String> Values=new ArrayList<String>();
        Values.clear();
        for  (int   n;   (n   =   in.read(b))   !=   -1;)   { 
                s1=new  String(b,   0,   n);
                out.append(s1);
        } 
        System.out.println(out);
String[] s13=s1.split("><");
String ifString=MonthsName+"Result";
    String TS="";
    String vs="";
    
    Boolean getValueBoolean=false;
for(int i=0;i<s13.length;i++){
TS=s13[i];
System.out.println(TS);
int j,k,l;
j=TS.indexOf(ifString);
k=TS.lastIndexOf(ifString);

if (j>=0)
{
System.out.println(j);
if (getValueBoolean==false)
{
getValueBoolean=true;
}
else {

}

if ((j>=0)&&(k>j))
{
 System.out.println("FFF"+TS.lastIndexOf("/"+ifString));
 //System.out.println(TS);
 l=ifString.length()+1;
 vs=TS.substring(j+l,k-2);
 //System.out.println("fff"+vs);
 Values.add(vs);
 System.out.println("退出"+vs);
getValueBoolean=false;
return   Values; 
}
 
}
if (TS.lastIndexOf("/"+ifString)>=0)
{
getValueBoolean=false;
return   Values; 
}
if ((getValueBoolean)&&(TS.lastIndexOf("/"+ifString)<0)&&(j<0))
{
 k=TS.length();
 //System.out.println(TS);
 vs=TS.substring(7,k-8);
 //System.out.println("f"+vs);
 Values.add(vs);
}

}

        return   Values; 
}
}

解决方案 »

  1.   

    解决了,需要在AndroidManifest.xml中添加:
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    以及在执行的Activity中onCreate里
    添加:
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    这是因为在老版本sdk中支持直接访问网络,但在高版本中需要获取权限及声明。这2天累死我了,身心疲惫啊。到处找解决办法。