GetHttpResponse警告:此AsyncTask类应该是静态的,否则可能会发生泄漏

时间:2018-01-31 07:47:59

标签: android android-asynctask

我不知道为什么这个错误会出现在我的Android工作室中,但它会打扰我。

以下是代码,我编写的内容

// JSON parse class started from here.
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
    public Context context;
    String JSonResult;
    List<Student> studentList;
    public GetHttpResponse(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {
        // Passing HTTP URL to HttpServicesClass Class.
        HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
        try
        {
            httpServicesClass.ExecutePostRequest();
            if(httpServicesClass.getResponseCode() == 200)
            {
                JSonResult = httpServicesClass.getResponse();
                if(JSonResult != null)
                {
                    JSONArray jsonArray = null;

                    try {
                        jsonArray = new JSONArray(JSonResult);
                        JSONObject jsonObject;
                        Student student;
                        studentList = new ArrayList<>();
                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            student = new Student();
                            jsonObject = jsonArray.getJSONObject(i);
                            // Adding Student Id TO IdList Array.
                            IdList.add(jsonObject.getString("id"));
                            //Adding Student Name.
                            student.NamaLatihan = jsonObject.getString("nama_latihan");
                            studentList.add(student);
                        }
                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        if(studentList != null) {
            progressBar.setVisibility(View.GONE);
            StudentListView.setVisibility(View.VISIBLE);
            ListAdapterClass adapter = new ListAdapterClass(studentList, context);
            StudentListView.setAdapter(adapter);
        }else
        {
            Toast.makeText(context, "Tidak ada data ditampilkan", Toast.LENGTH_LONG).show();
        }

    }
}

有人知道如何修复此代码吗? 我尝试添加引用此https://stackoverflow.com/a/46166223的静态,但GetHttpResponse中的所有代码都会出错,请我帮忙

0 个答案:

没有答案