gpt4 book ai didi

java - 无法使用异步任务复制程序中给出 URL 的网页的 html 代码。安卓工作室3.4.2

转载 作者:行者123 更新时间:2023-12-02 00:43:02 27 4
gpt4 key购买 nike

我想使用异步任务使用并行线程下载网页的 HTML 内容。我从在线教程中学习了代码,我的代码与讲座中的代码完全一样,运行良好。

异步任务中的 try 命令不起作用,doInbackGround() 方法通过 catch 返回“失败”。

我已经在方法中打印了 URL 名称,并且它显示在日志中。

I have provided the internet permission also through AndroidManifest.xml.

My primary guess is following three lines are creating problem though I would appreciate expert advice.



urlConnection=(HttpURLConnection) url.openConnection();

InputStream in=urlConnection.getInputStream();

InputStreamReader reader = new InputStreamReader(in);

package com.example.webdownload;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends AppCompatActivity {


public class DownLoadTask extends AsyncTask<String,Void,String> {

@Override
protected String doInBackground(String... urls) {

URL url;
String result="";
HttpURLConnection urlConnection=null;

try{

url=new URL(urls[0]);
Log.i("String",urls[0]); // this is getting printed

urlConnection=(HttpURLConnection) url.openConnection();
InputStream in=urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);

int data=reader.read();
Log.i("Data",Integer.toString(data)); //this is not getting
printed
while (data!=-1)
{
char current = (char) data;
result+=current;
data=reader.read();

}

return result;

}catch(Exception e)
{
e.printStackTrace();
return "Failed";
}

}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DownLoadTask task=new DownLoadTask();


String result=null;
try {
result = task.execute("https://www.ecowebhosting.co.uk/").get();

}catch (Exception e)
{
e.printStackTrace();
}
Log.i("Result",result);

}
}

我无法在这里发布日志

最佳答案

Since I could not post logs in my question I am posting them here:

These are the logs between URL printing in logs and to the Failed return through the method

I/String: https://www.ecowebhosting.co.uk/
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/System.err: java.net.SocketException: socket failed: EPERM (Operation not permitted)
W/System.err: at java.net.Socket.createImpl(Socket.java:492)
W/System.err: at java.net.Socket.getImpl(Socket.java:552)
W/System.err: at java.net.Socket.setSoTimeout(Socket.java:1180)
W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
W/System.err: at com.example.webdownload.MainActivity$DownLoadTask.doInBackground(MainActivity.java:35)
at com.example.webdownload.MainActivity$DownLoadTask.doInBackground(MainActivity.java:18)
W/System.err: at android.os.AsyncTask$3.call(AsyncTask.java:378)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
W/System.err: at java.lang.Thread.run(Thread.java:919)
I/Result: Failed
D

关于java - 无法使用异步任务复制程序中给出 URL 的网页的 html 代码。安卓工作室3.4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57920684/

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