我收到以下错误:
10-24 13:31:14.252: E/AndroidRuntime(21983): FATAL EXCEPTION: main
10-24 13:31:14.252: E/AndroidRuntime(21983): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.campusfqm.cfqm/br.com.campusfqm.cfqm.Launch}: android.os.NetworkOnMainThreadException
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.os.Looper.loop(Looper.java:137)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread.main(ActivityThread.java:4507)
10-24 13:31:14.252: E/AndroidRuntime(21983): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:31:14.252: E/AndroidRuntime(21983): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 13:31:14.252: E/AndroidRuntime(21983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-24 13:31:14.252: E/AndroidRuntime(21983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-24 13:31:14.252: E/AndroidRuntime(21983): at dalvik.system.NativeStart.main(Native Method)
10-24 13:31:14.252: E/AndroidRuntime(21983): Caused by: android.os.NetworkOnMainThreadException
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-24 13:31:14.252: E/AndroidRuntime(21983): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-24 13:31:14.252: E/AndroidRuntime(21983): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-24 13:31:14.252: E/AndroidRuntime(21983): at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-24 13:31:14.252: E/AndroidRuntime(21983): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-24 13:31:14.252: E/AndroidRuntime(21983): at br.com.campusfqm.cfqm.Launch.onCreate(Launch.java:54)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.Activity.performCreate(Activity.java:4465)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-24 13:31:14.252: E/AndroidRuntime(21983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
10-24 13:31:14.252: E/AndroidRuntime(21983): ... 11 more
指向
urlConnection.connect();
这是完整的代码
URL url = new URL("http://www.google.com/");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,fileName);
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
//this is where you would do something to report the prgress, like this maybe
Log.v("cfqm",""+downloadedSize+"/"+totalSize);
}
//close the output stream when done
fileOutput.close();
答案 0 :(得分:6)
它是android.os.NetworkOnMainThreadException
表示您正在主线程上进行网络连接。使用AsyncTask or thread
执行网络操作而不是UI线程
答案 1 :(得分:3)
Caused by: android.os.NetworkOnMainThreadException
所有网络操作都需要在单独的线程上执行,不应该在主线程上。您需要使用Async Task来执行网络操作。
关于如何使用Asynch任务
,这是很好的android tutorial