Java 异步任务片段背景数据

Java 异步任务片段背景数据,java,android,android-recyclerview,android-asynctask,Java,Android,Android Recyclerview,Android Asynctask,对于如何在不减慢主UI线程的情况下将大量模板JSON数据从这个网站加载到我的应用程序中,我感到非常陌生和困惑。我可以将要从JSON响应加载的数据加载到我的recycler视图中,但是单击显示该数据的底部导航选项卡需要很长时间才能加载到页面上 我理解这是因为数据没有正确加载到后台线程,这就是我感到困惑的地方 我希望能够从后台任务返回一个ArrayList,并将填充的ArrayList作为我的recyclerview适配器传递。以下是包含AsyncTask和recyclerview代码的片段: pu

对于如何在不减慢主UI线程的情况下将大量模板JSON数据从这个网站加载到我的应用程序中,我感到非常陌生和困惑。我可以将要从JSON响应加载的数据加载到我的recycler视图中,但是单击显示该数据的底部导航选项卡需要很长时间才能加载到页面上

我理解这是因为数据没有正确加载到后台线程,这就是我感到困惑的地方

我希望能够从后台任务返回一个ArrayList,并将填充的ArrayList作为我的recyclerview适配器传递。以下是包含AsyncTask和recyclerview代码的片段:

public class LoadJSONAsyncTask extends AsyncTask<String, String, ArrayList<String>>
    {

        protected void onPreExecute()
        {


        }

        public String getJSONFromURL(String inUrl) throws IOException
        {
            URL url2 = new URL(inUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
            StringBuilder total = new StringBuilder();

            try
            {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                BufferedReader r = new BufferedReader(new InputStreamReader(in));

                for(String line; (line = r.readLine()) != null;)
                {
                    total.append(line).append('\n');
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            finally
            {
                urlConnection.disconnect();
            }

            return total.toString();
        }

        @Override
        protected ArrayList<String> doInBackground(String... params)
        {
            LoadJSONAsyncTask jParser = new LoadJSONAsyncTask();
            JSONArray array = null;
            JSONObject json = null;
            String url = "https://baconipsum.com/api/?type=meat-and-filler";

            StringBuilder returnMe = new StringBuilder();
            results = new ArrayList<>();

            try {
                array = new JSONArray(jParser.getJSONFromURL(url));
                for(int n = 0; n < array.length(); n++)
                {
                    try {
                        json = new JSONObject();

                        json.put("name", jParser.getJSONFromURL(url));
                        results.add(jParser.getJSONFromURL(url));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



            return results;
        }

        protected void onPostExecute()
        {

        }

    }
公共类LoadJSONAsyncTask扩展了AsyncTask
{
受保护的void onPreExecute()
{
}
公共字符串getJSONFromURL(字符串inUrl)引发IOException
{
URL url2=新URL(inUrl);
HttpURLConnection urlConnection=(HttpURLConnection)url2.openConnection();
StringBuilder总计=新StringBuilder();
尝试
{
InputStream in=new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r=新的BufferedReader(新的InputStreamReader(in));
for(字符串行;(line=r.readLine())!=null;)
{
总计.append(行).append('\n');
}
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
urlConnection.disconnect();
}
返回total.toString();
}
@凌驾
受保护的ArrayList doInBackground(字符串…参数)
{
LoadJSONAsyncTask jParser=新建LoadJSONAsyncTask();
JSONArray数组=null;
JSONObject json=null;
字符串url=”https://baconipsum.com/api/?type=meat-和填充物”;
StringBuilder returnMe=新建StringBuilder();
结果=新的ArrayList();
试一试{
数组=新的JSONArray(jParser.getJSONFromURL(url));
对于(int n=0;n
在我的片段中的onCreateView()方法中,recyclerview的定义如下:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        try
        {
            results = new LoadJSONAsyncTask().execute(url).get();
            for(int i = 0; i < results.size(); i++)
            {
                newsFeedList.add(new NewsFeed(results.get(i)));
            }
        } catch (ExecutionException e) {

        } catch (InterruptedException e) {e.printStackTrace();
            e.printStackTrace();
        }
        View view = inflater.inflate(R.layout.fragment_feeds, container, false);

        newsFeedList = new ArrayList<>();

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        final NewsAdapter mAdapter = new NewsAdapter(getActivity(), newsFeedList);
        recyclerView.setAdapter(mAdapter);


        return view;
    }
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
尝试
{
结果=new LoadJSONAsyncTask().execute(url.get();
对于(int i=0;i

我有一种感觉,在onCreateView()方法中,调用arraylist来执行数据的方式就是挂起线程上的数据。一个清晰的解释和例子将不胜感激

在LoadJSONAsyncTask结束后调用Fragment

覆盖
LoadJSONAsyncTask
onPostExecute
,并将所有UI更新放在其中

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_feeds, container, false);

        newsFeedList = new ArrayList<>();

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        final NewsAdapter mAdapter = new NewsAdapter(getActivity(), newsFeedList);
        recyclerView.setAdapter(mAdapter);

        LoadJSONAsyncTask asyncTask = new LoadJSONAsyncTask() {
        @Override
        void onPostExecute(ArrayList<String> results) {
                // update your adapter here with the result

                for(int i = 0; i < results.size(); i++)
                {
                    newsFeedList.add(new NewsFeed(results.get(i)));
                }

                // notify adapter here to update the view
                mAdapter.notifyDataSetChanged()
            }
        }

        asyncTask.execute(url);

        return view;
    }

@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u feed,container,false);
newsFeedList=newarraylist();
recyclerView=(recyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
setLayoutManager(新的LinearLayoutManager(getActivity());
final NewsAdapter mAdapter=NewsAdapter(getActivity(),newsFeedList);
recyclerView.setAdapter(mAdapter);
LoadJSONAsyncTask asyncTask=新建LoadJSONAsyncTask(){
@凌驾
void onPostExecute(ArrayList结果){
//在此处使用结果更新适配器
对于(int i=0;i
.get()
挂起主线程


下面介绍如何正确使用
AsyncTask

我会在onCreateView方法中执行上述代码吗?我已经更新了我的答案,因此它显示了您应该执行的操作。谢谢您,我将在明天早上醒来时查看它,非常感谢!