English 中文(简体)
我的JSON Volley请求如果是Aynchronous的,那么它为什么会给国家情报局,而把数据输入我的数据库的工作是在AyncTask(JAVA)范围内进行的。
原标题:Why is my JSON Volley request causing ANR if it is Asynchronous in itself and the data transfer into my database is handled within an AsyncTask?(JAVA)

就我而言,这是我第一次与JSON、Volley或AsyncTask合作。

面对一个编码挑战,我必须从尿素中下载一个JSON阵列,并在进行一些编辑和过滤后显示项目清单。 我通过瓦 Java的Volley与JSON案连接。 我在我的JSONArrayRequest的“Response”方法中,拿走了JSON的物品,将其改装成java的物品,然后将这些物品添加到一个会议室KQ数据库。 这一繁重任务导致国家情报和安全局的成立,因此,我把转换和转入数据库的工作转到ASyncTask。 它仍在造成国家情报局。 如果所有重型方法在阿森古兰经发生,这种可能性如何? 相关守则和以下守则的解释:

这是我对我的控制人员采取的直线方法,我称之为AsyncTask:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .build());


        repository = new Repository(getApplication());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list_screen);


        itemRV = findViewById(R.id.itemListRecyclerView);
        progressBar = findViewById(R.id.progressBar1);

        RequestQueue queue1 = Volley.newRequestQueue(ItemListScreen.this);
        queue = queue1;

        final ItemAdapter itemAdapter = new ItemAdapter(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        RecyclerView recyclerView  = findViewById(R.id.itemListRecyclerView);
        recyclerView.setAdapter(itemAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        if(repository.getAllItems().isEmpty()) {
            //moves the download of the json file offof the UI thread
            startAsyncTask();
        }
        //fills GUI table with database data

        items = repository.getAllItems();
        itemAdapter.setItems(items);
    }

PHOTO VERSION:

https://i.stack.imgur.com/hHsoL.png” rel=“nofollow noreferer”>。

下面是利用新创建的物品执行和填充会议室KQ数据库的方法。 这一点在AsyncTask内部提出。

private void getData () throws InterruptedException {
        //fills database with JSON objects
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject responseObj = response.getJSONObject(i);
                        int id = responseObj.getInt("id");
                        int listId = responseObj.getInt("listId");
                        String name = responseObj.getString("name");
                        Item item = new Item(id, listId, name);
                        repository.insert(item);

                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                        Toast.makeText(ItemListScreen.this, "Failed to get the data", Toast.LENGTH_SHORT).show();
                    }
                });
        //Thread.sleep(1000);
            queue.add(jsonArrayRequest);
    }

PHOTO VERSION:

https://i.stack.imgur.com/ZFqfM.png” rel=“nofollow noreferer”>。

这是我的阿辛克·塔克,用我的控制器的OnCreate方法。 预先审查应转而讨论进展情况,然后由DoInBackground进行数据转让,然后,员额应在DoInBackground完成后进行,并放弃进展。 之后,该法典应重新采用“OnCreate”方法,并在“ASyncTask”号以下填写:

public void startAsyncTask(){
        ItemsAsyncTask itemsAsyncTask = new ItemsAsyncTask(this);
        //not sure what this integerparameter does but a youtube video said this was very important to have.
        itemsAsyncTask.execute(10);
    }
    private static class ItemsAsyncTask extends AsyncTask<Integer, Integer, String>{

        private WeakReference<ItemListScreen> reference;

        ItemsAsyncTask(ItemListScreen activity){
            reference = new WeakReference<ItemListScreen>(activity);
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            ItemListScreen activity = reference.get();
            if (activity == null || activity.isFinishing()){
            return;
            }
            //This should begin the progress bar on the screen, which should continue throughout the doInBackground method.
            activity.itemRV.setVisibility(View.INVISIBLE);
            activity.progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(Integer... integers) {
            //this calls the getData method that performs the data transfer away from the UI thread.
            try {
                ItemListScreen activity = reference.get();
                if (activity == null || activity.isFinishing()){
                    return "return";
                }
                activity.getData();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "return";        }



        @Override
        protected void onPostExecute(String s) {
            //After the doInBackground method is complete, this should remove the progress bar
            // and then continue on to the rest of the OnCreate method where the GUI table will be filled witht he database objects
            super.onPostExecute(s);
            ItemListScreen activity = reference.get();
            if (activity == null || activity.isFinishing()){
                return;
            }
            activity.itemRV.setVisibility(View.VISIBLE);
            activity.progressBar.setVisibility(View.INVISIBLE);
        }
    }

PHOTO VERSION:

https://i.stack.imgur.com/qSvwK.png” rel=“nofollow noreferer”>。

因此,摘要 我的格利请求造成国家自然资源局,尽管它属于ASyncTask公司。 在ANR(我对位于A/61/3室的数据库进行观察)时,JSON仍被下载到数据库中,而甲状腺素装置和刺.装置正在显示一种流行的信息,说“应用不是反应的”,但“预先和邮政”方法在屏幕上即刻出现,然后立即再次出现。 因此,在DoInBackground完成或做一些事情之前,它就象邮政方法一样。 是否有任何人知道这种情况发生?

此外,在申请首次大量下载后,一旦进一步开放数据库,国家情报和安全局即已全部启用。 这只是第一次使用该数字,必须履行从月球下载JSON的挑战要求。

我对Async进行了审判,但ANR仍在审理之中。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签