android:如何从链接获取数据并读取它

时间:2014-12-27 00:57:24

标签: java android web-services

我正在尝试读取已添加到Dropbox公用文件夹的.txt和.csv文件。我能够读取数据并在我的电脑上用java打印但是当我想在Android上阅读和打印相同的数据时,我遇到了一些问题而且我不确定我错过了什么或做错了,这是我的代码(这与我在电脑上运行的程序相同):

        try
        {
            URL dataLink = new URL(link);
            URLConnection dlc = dataLink.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(dlc.getInputStream())); //error happening here
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
            {
                 System.out.println(inputLine);
            }

        }
        catch (MalformedURLException e) 
        {

            e.printStackTrace();
            return;
        } catch (IOException e) 
        {
            e.printStackTrace();
            return;
        }

这也是我得到的错误:


12-26 16:43:31.295: D/AndroidRuntime(4988): Shutting down VM
12-26 16:43:31.295: W/dalvikvm(4988): threadid=1: thread exiting with uncaught exception (group=0x41af5da0)
12-26 16:43:31.295: E/AndroidRuntime(4988): FATAL EXCEPTION: main
12-26 16:43:31.295: E/AndroidRuntime(4988): Process: com.src.readingdata, PID: 4988
12-26 16:43:31.295: E/AndroidRuntime(4988): android.os.NetworkOnMainThreadException
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.src.readingdata.GetData.onClick(GetData.java:65)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.view.View.performClick(View.java:4753)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.view.View$PerformClick.run(View.java:19562)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.os.Handler.handleCallback(Handler.java:733)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.os.Looper.loop(Looper.java:146)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at android.app.ActivityThread.main(ActivityThread.java:5635)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invoke(Method.java:515)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
12-26 16:43:31.295: E/AndroidRuntime(4988):     at dalvik.system.NativeStart.main(Native Method)

我添加的权限是INTERNET,ACCESS_NETWORK_STATE,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE。

任何帮助都会非常感谢!

这也是我第一次在这里发帖,如果我没有正确做某事就很抱歉。

1 个答案:

答案 0 :(得分:2)

您需要使用AsyncTask在单独的线程中执行网络操作。 Android不允许主线程中的网络操作来提供良好的用户体验。

请查看本指南: http://developer.android.com/training/articles/perf-anr.html