gpt4 book ai didi

java - Android 中的 HttpConnect 失败

转载 作者:行者123 更新时间:2023-12-01 17:42:19 35 4
gpt4 key购买 nike

使用已接受的答案 here ,我正在尝试下载 xml 文件:

public void get_flickr(double lat,double lang) {
String flickr_api="https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=";
String flickr_key="key";
String flickr_opt="&format=rest&accuracy=11&content_type=1&privacy_filter=1&geo_context=2";
String urls = flickr_api+flickr_key+"&lat="+lat+"&long="+lang+flickr_opt;
try {
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
File file = new File(getApplicationContext().getFilesDir(), "flickr.xml");
FileOutputStream fOut =new FileOutputStream(file);
InputStream fInp = connection.getInputStream();
int totalSize = connection.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 = fInp.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fOut.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;

}
//close the output stream when done
fOut.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

出现错误:

android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:621)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:182)
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:145)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)
at com.example.phocast.MainActivity.get_flickr(MainActivity.java:336)
at com.example.phocast.MainActivity.setupViewPager(MainActivity.java:189)
at com.example.phocast.MainActivity.access$100(MainActivity.java:60)
at com.example.phocast.MainActivity$2.onComplete(MainActivity.java:237)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

地点:

line 336 :     connection.connect();
line 189 : get_flickr(Lat, Long);

我在这里做错了什么?

请帮忙

最佳答案

问题是您正在尝试在主线程上运行网络操作,这是一个阻塞操作,因此请使用 AsyncTask并在后台完成所有工作(另一个线程)。

另请检查以下内容 answer.

关于java - Android 中的 HttpConnect 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59543545/

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