如何在ui屏幕上修复3秒钟停止服务的主线程?

时间:2019-02-22 13:35:10

标签: java android android-volley

我正在为我的移动应用程序编码服务。它用于通过REST-API2从我的wordpress新闻网站提取数据。我的服务正在运作。但是当执行Volley库StringRequest时,我的主线程(UI)冻结了3-5秒。在工作之后。但这在每次提取数据时都会重复。

那是我的代码:

public class myPullData extends AsyncTask<Integer, Void, Void>{


    @Override
    protected Void doInBackground(final Integer... params) {
        RequestQueue rQueue = Volley.newRequestQueue(context);
        String url = "https://haberyaziyorum.com/wp-json/wp/v2/posts?categories="+params[0]+"&page=1";
        dataList = new ArrayList<>();
        System.out.println(url);
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                gson = new Gson();
                list = (List) gson.fromJson(s, List.class);
                //mapctrl = (map)list.get(0);
                //string x = (string) mapctrl.get("code");
                //if(x == null)
                //alList.addAll(list);
                postTitle = new String[list.size()];
                postImg = new String[list.size()];

                for(int i=0;i<list.size();++i){
                    mapPost = (Map<String, Object>)list.get(i);
                    mapTitle = (Map<String, Object>) mapPost.get("title");
                    mapContent = (Map<String, Object>) mapPost.get("content");
                    postTitle[i] = (String) mapTitle.get("rendered");
                    postImg[i] = (String) mapPost.get("jetpack_featured_media_url");
                    postID = ((Double)mapPost.get("id")).intValue();
                    postDate = mapPost.get("date").toString();
                    postContent = mapContent.get("rendered").toString();
                    Data d = new Data(params[0],postID,postTitle[i],postContent,postDate,postImg[i]);
                    //System.out.println(postID+"-"+postTitle[i]+postDate);
                    dataList.add(d);
                    // System.out.println(dataList.size());
                }
                aa();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("HATA-HATA-HATA");
                System.out.println(volleyError.toString());
                NetworkResponse response = volleyError.networkResponse;
                if (response != null){
                    System.out.println(response.statusCode);
                }
            }
        });
        request.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 50000;
            }

            @Override
            public int getCurrentRetryCount() {
                return 50000;
            }

            @Override
            public void retry(VolleyError error) throws VolleyError {

            }
        });

        rQueue.add(request);
        return null;
    }

}

控制台说:

I/Choreographer: Skipped 227 frames!  The application may be doing too much work on its main thread.

我该如何解决此问题?

0 个答案:

没有答案