我厌倦了oracle的例子,用android读取URLConnection 这是代码
``try{
URL oracle = new URL("http://www.oracle.com/");
Proxy proxy = new Proxy(Proxy.Type.HTTP,newInetSocketAddress("192.168.3.254",3128)); // or whatever your proxy is
URLConnection yc = oracle.openConnection(proxy);
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
Toast.makeText(this, inputLine, Toast.LENGTH_SHORT);
in.close();
}
catch(IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我注意到我的代理模拟器设置已配置 我有这个日志错误
> 04-20 14:20:21.842:D / AndroidRuntime(988):关闭VM 04-20 14:20:21.842:W / dalvikvm(988):threadid = 1:线程退出时未捕获异常(group = 0x409c01f8) 04-20 14:20:21.862:E / AndroidRuntime(988):致命异常:主要 04-20 14:20:21.862:E / AndroidRuntime(988):java.lang.RuntimeException:无法启动活动ComponentInfo {connect.oracle.java/connect.oracle.java.ConnectActivity}:android.os.NetworkOnMainThreadException 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread.access $ 600(ActivityThread.java:123) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.os.Handler.dispatchMessage(Handler.java:99) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.os.Looper.loop(Looper.java:137) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread.main(ActivityThread.java:4424) 04-20 14:20:21.862:E / AndroidRuntime(988):at java.lang.reflect.Method.invokeNative(Native Method) 04-20 14:20:21.862:E / AndroidRuntime(988):at java.lang.reflect.Method.invoke(Method.java:511) 04-20 14:20:21.862:E / AndroidRuntime(988):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 04-20 14:20:21.862:E / AndroidRuntime(988):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-20 14:20:21.862:E / AndroidRuntime(988):at dalvik.system.NativeStart.main(Native Method) 04-20 14:20:21.862:E / AndroidRuntime(988):引起:android.os.NetworkOnMainThreadException 04-20 14:20:21.862:E / AndroidRuntime(988):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 04-20 14:20:21.862:E / AndroidRuntime(988):at java.net.InetAddress.getHostByAddrImpl(InetAddress.java:440) 04-20 14:20:21.862:E / AndroidRuntime(988):at java.net.InetAddress.getHostName(InetAddress.java:313) 04-20 14:20:21.862:E / AndroidRuntime(988):at java.net.InetSocketAddress.getHostName(InetSocketAddress.java:156) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpConnection $ Address。(HttpConnection.java:322) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpConnection.connect(HttpConnection.java:96) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 04-20 14:20:21.862:E / AndroidRuntime(988):at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 04-20 14:20:21.862:E / AndroidRuntime(988):在connect.oracle.java.ConnectActivity.onCreate(ConnectActivity.java:22) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.Activity.performCreate(Activity.java:4465) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 04-20 14:20:21.862:E / AndroidRuntime(988):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 04-20 14:20:21.862:E / AndroidRuntime(988):... 11 more
提前谢谢你
答案 0 :(得分:1)
您已Strict Mode开启。当你阻止UI线程执行缓慢的IO操作时,它会抱怨。
关闭严格模式,或者 - 总是更好的选择 - 将IO代码移动到后台线程。
答案 1 :(得分:0)
您是否在清单文件中添加了互联网权限?
<uses-permission android:name="android.permission.INTERNET" />
答案 2 :(得分:0)
将IO代码移至背景线程(或)Asynch任务(如果符合您的要求)。在主线程上使用网络调用不是最佳做法。请参阅此write-up。