如何在MainActivity / Fragment之外解析网站

时间:2016-09-04 13:28:44

标签: java android android-fragments networkonmainthread

我编写了一个解析网站并返回ArrayList的函数。稍后将此ArrayList传递给片段,该片段使其适应listview。我的问题是,该解析器在MainActivity中工作,因此它导致:android.os.NetworkOnMainThreadException

我的问题是,如何让解析器在MainActivity之外运行,但仍能返回ArrayList?

这是我的片段代码:

public class NewsFragment extends Fragment {

ArrayList <ParsedWebData> list = new ArrayList<ParsedWebData>();


public NewsFragment() {
    // Required empty public constructor
}


public static NewsFragment newInstance(Context context) {
    NewsFragment fragment = new NewsFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_news, container, false);



    final ListView listView = (ListView) view.findViewById(R.id.listViewNews);
    ParseWebSite parser = new ParseWebSite();

    try{
        list = parser.parseWebSite();
    } catch (IOException e) {
        e.printStackTrace();
    }




    CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(getContext(), R.id.listViewNews, list);
    listView.setAdapter(customListViewAdapter);

    final ArrayList<ParsedWebData> linksFinal = list;

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {

            ParsedWebData singleData = linksFinal.get(position);
            String url = singleData.getUrl();

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);


        }
    });

    return view;
}
}

我的Parser课程:

public class ParseWebSite {



public static ArrayList<ParsedWebData> parseWebSite() throws IOException {

    ArrayList<ParsedWebData> listOfLinks = new ArrayList<ParsedWebData>();

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);



    String url = "http://www.portal.pwr.wroc.pl/box_main_page_news,241.dhtml?limit=10";
    String baseURL = "http://www.portal.pwr.wroc.pl/";
    Document doc = Jsoup.connect(url).get();
    Elements links = doc.select(".title_1 > a");
    Elements descriptions = doc.select(".nitemcell2 > div");



    for (int i = 0; i < links.size() && i < descriptions.size(); i++) {
        ParsedWebData data = new ParsedWebData();
        data.title = links.get(i).text().trim();
        data.url = baseURL + links.get(i).attr("href");
        data.description = descriptions.get(i).text().trim();

        listOfLinks.add(data);

    }

    return listOfLinks;
}

}

如果这很重要,我的ParsedWebData类:

public class ParsedWebData {

String title;
String url;
String description;

public ParsedWebData() {

}

public ParsedWebData(String title, String url, String description) {
    this.title = title;
    this.url = url;
    this.description = description;
}

public String getTitle() {
    return title;
}


public String getUrl() {
    return url;
}

public String getDescription() {
    return description;
}


}

我尝试使用AsyncTask()和Thread(),到目前为止没有任何运气。此外,我想在应用程序启动后立即解析此网站。

感谢您的帮助。

0 个答案:

没有答案