无法在setOnClickListener中执行Asynctask

时间:2017-08-15 02:53:32

标签: java android

我在这一行上有错误 - > new GetHttpResponse(this).execute();

当它放在按钮外面时没有错误

我从http://www.android-examples.com/create-dynamic-listview-using-json-parsing-php-mysql/获得了此代码,我刚刚添加了按钮,以便在更新数据库时刷新它

 public class MainActivity extends Activity {

    ListView listCollege;
    ProgressBar proCollageList;
    Button bton;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    bton = (Button) findViewById(R.id.button);
    listCollege = (ListView)findViewById(R.id.listCollege);
    proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
    new GetHttpResponse(this).execute();

    bton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "List is refreshed", Toast.LENGTH_SHORT).show();
            new GetHttpResponse(this).execute();
        }});
}



    private class GetHttpResponse extends AsyncTask<Void, Void, Void> 
    {
        private Context context;
        String result;
        List<benedict.com.listviewpractice.cources> collegeList;
        public GetHttpResponse(Context context)
        {
            this.context = context;
        }

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

        @Override
        protected Void doInBackground(Void... arg0) 
        {
            HttpService httpService = new HttpService("http://10.0.2.2/courses.php");
            try 
            {
                httpService.ExecutePostRequest();

                if(httpService.getResponseCode() == 200)
                {
                    result = httpService.getResponse();
                    Log.d("Result", result);
                    if(result != null)
                    {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(result);

                            JSONObject object;
                            JSONArray array;
                            benedict.com.listviewpractice.cources college;
                            collegeList = new ArrayList<benedict.com.listviewpractice.cources>();
                            for(int i=0; i<jsonArray.length(); i++)
                            {
                                college = new benedict.com.listviewpractice.cources();
                                object = jsonArray.getJSONObject(i);

                                college.cources_name = object.getString("cource_name");
                                college.cources_description = object.getString("cources_description");

                                collegeList.add(college);
                            }
                        }
                        catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                else
                {
                    Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            } 
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) 

        {
            proCollageList.setVisibility(View.GONE);
            listCollege.setVisibility(View.VISIBLE);
            if(collegeList != null)
            {
            ListAdapter adapter = new ListAdapter(collegeList, context);
            listCollege.setAdapter(adapter);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在OnClickListener中,this表示OnClickListener的实例,而不是您期望的上下文对象,因此this应为MainActivity.this

new GetHttpResponse(MainActivity.this).execute();