尝试http连接时android.os.networkNetworkOnMainThreadException

时间:2014-04-30 20:52:47

标签: android http networking

所以我得到了android.os.NetworkOnMainThreadException,我似乎无法找到错误。我已经搜索过它,大多数人似乎都得到它,因为他们试图在主线程上访问网络。我创建了#34; AsyncTask"制作一个不同的主题并让它在后台运行,所以除非我做错了,否则我不确定我能改变什么。

package rafa.weatherapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity

{

    TextView http;
/** Called when the activity is first created. 
 * @param runnable */

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);         
        http = (TextView) findViewById(R.id.http_display);

        GetHttp yes = new GetHttp();

        http.setText(yes.doInBackground());



        //connect.execute();


    }


    private class GetHttp extends AsyncTask<Object, Void, String>
    {
        @Override
        protected String doInBackground(Object... arg0) {
            int responseCode= -1;
            int identity = 0;
            try{
                //define URL that information will come in from
                URL url = new URL("http://openweathermap.org/data/2.3/forecast/city?id=524901&APPID=111111111");
                //establish HttpURL Connection
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();

                //check to make sure it connected
                responseCode=connection.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK);

                //Take in the data 
                InputStream in = connection.getInputStream();
                Reader reader = new InputStreamReader(in);
                int contentLength = connection.getContentLength();          
                char charArray[] = new char[contentLength];

                reader.read(charArray);
                String responseData = new String(charArray);

                Log.v(responseData, null);





            }
            catch(IOException e ){
                return "Unable to retrieve webpage, URL may be invalid";
            }


            return null;

        }

    };
}

我的错误如下:

04-30 15:33:02.371: W/dalvikvm(17021): threadid=1: thread exiting with uncaught exception (group=0x4206d700)
04-30 15:33:02.376: E/AndroidRuntime(17021): FATAL EXCEPTION: main
04-30 15:33:02.376: E/AndroidRuntime(17021): java.lang.RuntimeException: Unable to start activity ComponentInfo{rafa.weatherapp/rafa.weatherapp.MainActivity}: android.os.NetworkOnMainThreadException
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread.access$700(ActivityThread.java:168)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.os.Looper.loop(Looper.java:137)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread.main(ActivityThread.java:5493)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at java.lang.reflect.Method.invoke(Method.java:525)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at dalvik.system.NativeStart.main(Native Method)
04-30 15:33:02.376: E/AndroidRuntime(17021): Caused by: android.os.NetworkOnMainThreadException
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at rafa.weatherapp.MainActivity$GetHttp.doInBackground(MainActivity.java:58)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at rafa.weatherapp.MainActivity.onCreate(MainActivity.java:36)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.Activity.performCreate(Activity.java:5372)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-30 15:33:02.376: E/AndroidRuntime(17021):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-30 15:33:02.376: E/AndroidRuntime(17021):    ... 11 more
04-30 15:38:02.746: I/Process(17021): Sending signal. PID: 17021 SIG: 9

1 个答案:

答案 0 :(得分:2)

请勿亲自致电doInBackground()。而是execute() AsyncTask。框架在后台线程上为您调用doInBackground()。使用结果更新onPostExecute()中的用户界面。