当前位置:编程学习 > JAVA >>

Android的URL连接不上,请问是什么问题?

我在服务器端配置好了,现在在客户端用http://192.168.74.46:8081/WebService1.asmx网站可以打开网址,也可以操作。在客户端,我在AndroidManifest中放了权限
<uses-permission android:name="android.permission.INTERNET"/> 
现在在调试代码上

String ServerUrl = "http://192.168.74.46:8081/WebService1.asmx"; 
 try {  
            URL url = new URL(ServerUrl);  
            HttpURLConnection con = (HttpURLConnection) url.openConnection();  
            byte[] bytes = requestData.getBytes("utf-8");  
            con.setDoInput(true);  
            con.setDoOutput(true);  
            con.setUseCaches(false);  
            con.setConnectTimeout(6000);// 设置超时时间   
            con.setRequestMethod("POST");  
            con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");  
            con.setRequestProperty("SOAPAction", soapAction);  
            con.setRequestProperty("Content-Length", "" + bytes.length);
            con.connect();
            OutputStream outStream = con.getOutputStream();  
            outStream.write(bytes);  
            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;  
        }  
    }  

在 con.connect();这步就出错连接不上网站。请问是什么原因呀,跪求大神指导。 --------------------编程问答-------------------- 不知道你用的无线网还是通过运营商? --------------------编程问答-------------------- 我用的是局域网,不是无线的。。。 --------------------编程问答-------------------- 报什么错误。 --------------------编程问答--------------------
引用 3 楼 rui888 的回复:
报什么错误。


他代码里把异常吃掉了 --------------------编程问答--------------------
就是这个在connection时抛异常在右上角有参数显示,显示是没有连接上。 --------------------编程问答-------------------- 你嘚跑到
 OutputStream outStream = con.getOutputStream();   这句才可以看到 到底是 true 还是false 吧。
你异常throw 出来。 --------------------编程问答-------------------- 解决了,是主线程异常
在Android的2.2/2.3中,直接使用HttpURLConnection或者HttpClient连接网络时正常,但是到了4.0以上就不报android.os.NetworkOnMainThreadException
网上也说了很多要加StrictMode,但是出错者加上去之后却显示StrictMode找不到这个类。这个问题是因为在当初建立Android项目的时候选择的Android版本是2.2了,而StrictMode是在Android 4.0(2.3-4.0之间没试过)以上才有的这个类,所以出现找不到这个类。
处理:
1、右击您的项目——>属性,如图所示:

选择adnroid4.0的版本,点击OK即可。
2、在您需要访问网络的Activity的onCreate方法中加入如下代码:
01.public static final boolean DEVELOPER_MODE = BuildConfig.DEBUG;

02.                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()

03.                     .detectDiskReads()

04.                     .detectDiskWrites()

05.                     .detectNetwork()   // or .detectAll() for all detectable problems

06.                     .penaltyLog()

07.                     .build());

08.                     StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()

09.                             .detectLeakedSqlLiteObjects()

10.                             .detectLeakedClosableObjects()

11.                             .penaltyLog()

12.                             .penaltyDeath()

13.                             .build());

14.super.onCreate();

15.setContentView(R.layout.main);
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,