Android,从3.1中的URL读取内容?
我有这个函数,它在2.3中运行良好,但在3.1中失败 有什么想法吗Android,从3.1中的URL读取内容?,android,url,Android,Url,我有这个函数,它在2.3中运行良好,但在3.1中失败 有什么想法吗 public static String readUrl( String url2 ){ String str = ""; try { // Create a URL for the desired page URL url = new URL( url2 ); // Read all the text returned by the server
public static String readUrl( String url2 ){
String str = "";
try {
// Create a URL for the desired page
URL url = new URL( url2 );
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
str = in.readLine();
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return str;
}
02-0221:58:09.100:E/AndroidRuntime(30937):致命异常:主
02-02 21:58:09.100:E/AndroidRuntime(30937):android.os.NetworkOnMainThreadException
02-02 21:58:09.100:E/AndroidRuntime(30937):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于java.net.InetAddress.lookupHostByName(InetAddress.java:477)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于java.net.InetAddress.getAllByName(InetAddress.java:249)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:69)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
02-02 21:58:09.100:E/AndroidRuntime(30937):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
02-02 21:58:09.100:E/AndroidRuntime(30937):在org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieverResponse(HttpURLConnectionImpl.java:1038)
02-02 21:58:09.100:E/AndroidRuntime(30937):位于org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
02-02 21:58:09.100:E/AndroidRuntime(30937):在java.net.URL.openStream(URL.java:645)自蜂巢以来,您必须异步执行网络操作(而不是在UI线程上)。例如,您可以使用。有关异步操作的更多信息,请参见: 如果您以异步方式进行网络操作,您的接口将是安全的,并且您不会有导致ANR对话框的风险