gpt4 book ai didi

java - 读取网站的 HTML 代码

转载 作者:太空宇宙 更新时间:2023-11-04 12:59:57 25 4
gpt4 key购买 nike

我正在尝试读取网站的 HTML 代码,因此我使用以下代码:我的 fragment 之一:

public class FragmentFavorites extends Fragment {
View view;
TextView text;
Homescreen home = new Homescreen();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.favorites,container, false);
text = (TextView) view.findViewById(R.id.textView2);
try {
text.setText(home.getHtml("http://pastebin.com/u7jHeNwf"));
} catch (IOException e) {
e.printStackTrace();
}
return view;
}
}

这是我指的 getHtml():

public static String getHtml(String url) throws IOException {
URLConnection connection = (new URL(url)).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();

InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder html = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
html.append(line);
}
in.close();

return html.toString();
}

不幸的是,每次我滚动到此 fragment/调用 getHTML 时,我的应用程序都会停止运行。有人知道我做错了什么吗?

最佳答案

    public class FragmentFavorites extends Fragment {
View view;
TextView text;
Homescreen home = new Homescreen();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.favorites,container, false);
text = (TextView) view.findViewById(R.id.textView2);
FetchHtml fetchHtml = new FetchHtml(getActivity().getApplicationContext(), FragmentFavorites.this);
fetchHtml.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://pastebin.com/u7jHeNwf");
return view;
}

public static class FetchHtml extends AsyncTask<String, Void, String> {

Context mContext;
WeakReference<FragmentFavorites> mClient;

public RegisterGcmTask(Context context, FragmentFavorites client) {
this.mContext = context;
this.mClient = new WeakReference<>(client);
}

@Override
protected String doInBackground(String... params) {
try {
return getHtml(params[0]);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String html) {
super.onPostExecute(token);
if (null != mClient && null != mClient.get()) {
if (null != html) {
mClient.get().text.setText(html);
} else {
mClient.get().text.setText("Error fetching html");
}
}
}

private static String getHtml(String url) throws IOException {
URLConnection connection = (new URL(url)).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();

InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder html = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
html.append(line);
}
in.close();

return html.toString();
}

}
}

关于java - 读取网站的 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022571/

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