Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决android.os.networkonmainthreadexception_Android_Jsoup - Fatal编程技术网

如何解决android.os.networkonmainthreadexception

如何解决android.os.networkonmainthreadexception,android,jsoup,Android,Jsoup,我想在地址中获取链接,我使用Jsoup和RecyclerView,所以我这样做: public static List<News> newsList(String url) { List<News> newsArrayList = new ArrayList<>(); try { Document document = Jsoup.connect().get(); Elements newsElements =

我想在地址中获取链接,我使用
Jsoup
RecyclerView
,所以我这样做:

public static List<News> newsList(String url) {
    List<News> newsArrayList = new ArrayList<>();
    try {
        Document document = Jsoup.connect().get();
        Elements newsElements = document.select(".boxMiddle .grpLinks a");
        int i = 1;
        for (Element newsElement : newsElements) {
            News news = new News();
            news.setId(i);
            news.setTitle(newsElement.text());
            news.setDate(newsElement.attr("title"));
            news.setUrl(Uri.parse("www.google.com"));
            newsArrayList.add(news);
            i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return newsArrayList;

}  
公共静态列表新闻列表(字符串url){
List newsArrayList=新建ArrayList();
试一试{
Document Document=Jsoup.connect().get();
Elements newsElements=document.select(“.boxMiddle.grpLinks a”);
int i=1;
for(新闻元素:新闻元素){
新闻=新新闻();
新闻,setId(i),;
news.setTitle(newelement.text());
news.setDate(newelement.attr(“title”);
news.setUrl(Uri.parse(“www.google.com”);
newsaraylist.add(新闻);
i++;
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回newsArrayList;
}  
但是,我得到了这个错误:
android.os.NetworkOnMainThreadException


如何解决此错误?

请使用AsyncTask。无法在UIThread上进行网络调用


对I/O使用
AsyncTask
。无法在主线程上执行联网

new AsyncTask<Void, Void, Void>(){

  public void doInBackground(Void... params){
    //I/O here
  }
}.execute();
newasynctask(){
公共void doInBackground(void…params){
//此处输入/输出
}
}.execute();