クラスHttpUrlConnectionを使用してAndroidに問題があります。私の方法は次のとおりです。
public InputStream test() {
URL url = null;
try {
url = new URL("http://www.android.com/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in= null;
try {
in = new BufferedInputStream(urlConnection.getInputStream());
}catch(IOException e){
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return in;
}
このメソッドを実行すると、アンドロイドはこの例外を生成します:
05-03 15:15:19.116: W/System.err(1085): android.os.NetworkOnMainThreadException 05-03 15:15:19.116: W/System.err(1085): android.os.StrictMode$AndroidBlockGuardPolicy で。 onNetwork(StrictMode.java:1099) 05-03 15:15:19.116: W/System.err(1085): java.net.InetAddress.lookupHostByName(InetAddress.java:391) 05-03 15:15:19.116: W/System.err(1085): java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 05-03 15:15:19.116: W/System.err(1085): java.net.InetAddress.getAllByName で(InetAddress.java:220) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpConnection で。(HttpConnection.java:71) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpConnection.(HttpConnection.java:50) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http で。 HttpConnection$Address.connect(HttpConnection.java:351) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 05-03 15:15:19.126: W/System. err(1085): libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpEngine.openSocketConnection で(HttpEngine.java:308) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpEngine.connect(HttpEngine.java:303) で 05-03 15:15:19.126 : W/System.err(1085): libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 05-03 15:15:19.126: W/System.err(1085): libcore.net で。 http.HttpEngine.sendRequest(HttpEngine.java:232) 05-03 15:15:19.126: W/System.err(1085): libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 05-03 で15:15:19.136: W/System.err(1085):libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 05-03 15:15:19.136: W/System.err(1085): com.BertacchiMazzoni.HTTPClient.HttpClientTutorial.test(HttpClientTutorial.java: 35) 05-03 15:15:19.136: W/System.err(1085): com.BertacchiMazzoni.HTTPClient.HTTPClientActivity.onCreate(HTTPClientActivity.java:18) 05-03 15:15:19.136: W/システム。 err(1085): android.app.Activity.performCreate(Activity.java:4465) 05-03 15:15:19.136: W/System.err(1085): android.app.Instrumentation.callActivityOnCreate(Instrumentation.java で:1049) 05-03 15:15:19.136: W/System.err(1085): android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-03 15:15:19.136: W/System.err (1085): android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05-03 15:15:19 で。136: W/System.err(1085): android.app.ActivityThread.access$600(ActivityThread.java:123) 05-03 15:15:19.136: W/System.err(1085): android.app. ActivityThread$H.handleMessage(ActivityThread.java:1147) 05-03 15:15:19.146: W/System.err(1085): android.os.Handler.dispatchMessage(Handler.java:99) 05-03 15: 15:19.146: W/System.err(1085): android.os.Looper.loop(Looper.java:137) 05-03 15:15:19.146: W/System.err(1085): android.app で.ActivityThread.main(ActivityThread.java:4424) 05-03 15:15:19.146: W/System.err(1085): java.lang.reflect.Method.invokeNative(ネイティブ メソッド) 05-03 15:15: 19.146: W/System.err(1085): java.lang.reflect.Method.invoke(Method.java:511) 05-03 15:15:19.146: W/System.err(1085): com.android で.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-03 15:15:19.146:W/System.err(1085): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-03 15:15:19.146: W/System.err(1085): dalvik.system で.NativeStart.main(ネイティブメソッド)
この問題を解決できますか? エラーはどこにありますか?
どうもありがとう!よろしく!