gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:07:21 26 4
gpt4 key购买 nike

我正在尝试读取已添加到 Dropbox 公共(public)文件夹中的 .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 VM12-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: main12-26 16:43:31.295: E/AndroidRuntime(4988): Process: com.src.readingdata, PID: 498812-26 16:43:31.295: E/AndroidRuntime(4988): android.os.NetworkOnMainThreadException12-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。

任何帮助将非常感谢!

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

最佳答案

您需要使用AsyncTask在单独的线程中执行网络操作。 Android不允许在主线程中进行网络操作,以保证良好的用户体验。

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

关于java - android:如何从链接获取数据并读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27663473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com