从URL获取.txt文件中的内容时出错

时间:2015-01-29 17:55:53

标签: java android url

URL scoreU = null;
    try {
        scoreU = new URL("http://m.uploadedit.com/b044/1422550899503.txt");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    BufferedReader in = null;
    try {
        in = new BufferedReader(
                new InputStreamReader(
                        scoreU.openStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }

    String inputLine;

    try {
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

我的代码与从网址获取文字有关。这是我第一次这样做,我无法理解我的错误。 (我不得不用try / catch包围几乎所有东西。

ERROR:



java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wc.gap.worldcupfixture/com.wc.gap.worldcupfixture.MainActivity}: android.os.NetworkOnMainThreadException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
            at java.net.InetAddress.getAllByName(InetAddress.java:215)
            at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
            at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
            at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
            at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
            at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190)
            at java.net.URL.openStream(URL.java:470)
            at com.wc.gap.worldcupfixture.MainActivity.onCreate(MainActivity.java:102)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)




new InputStreamReader(scoreU.openStream()));

2 个答案:

答案 0 :(得分:0)

使用此代码: -

try {
        URL url = new URL("http://m.uploadedit.com/b044/1422550899503.txt");
        URLConnection urlConnection = url.openConnection();
        HttpURLConnection connection = null;
        connection = (HttpURLConnection) urlConnection;

        BufferedReader in = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String urlString = "";
        String current;
        while ((current = in.readLine()) != null) {
            urlString += current;
        }
        System.out.println(urlString);
    } catch (IOException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

Android不允许主线程上的网络请求,因为它可能导致UI无响应。相反,你必须在后台线程上完成工作,但是使用AsyncTask这很容易,例如:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...

        // Start the task here
        new URLDataLoader().execute(theURL);
    }

    private class URLDataLoader extends AsyncTask<URL, Void, String> {
        @Override
        protected String doInBackground(URL... params) {
            URL theURL = params[0];
            // Do your network stuff here to get the data
            return theData;
        }

        @Override
        protected void onPostExecute(String data) {
            // Use the data here
        }
    }
}