Get the data from the array list of onPost run the asynchronous task method

advertisements

I am using asynck task to get the json and parse it.The value of parsed json I am storing it in ArrayList. Now what I want is that i want to us this Arraylist in different class.I have made a method getList() to get the Arraylist but there I am getting null value

Async Class

    public class SearchJobAsync extends AsyncTask<String, String, String> {
    private String response;
    Context c;
    SearchModel data;
    ArrayList<SearchModel> values;
    getArrayList list;

    public SearchJobAsync(Context c, getArrayList list) {
        this.c = c;
        this.list = list;
    }

    public interface getArrayList {
        public void getList(ArrayList<SearchModel> data);

    }

    public SearchJobAsync(Context c) {
        this.c = c;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute ();
        CommonFunctions.showProgress (c, "Please Wait...", true);

    }

    @Override
    protected void onPostExecute(String s) {
        values = new ArrayList<SearchModel> ();

        super.onPostExecute (s);
        if (!s.trim ().contains ("Table")) {
            Crouton.makeText ((android.app.Activity) c, "Nothing found", Style.INFO).show ();
        } else {
            try {

                JSONObject jsonObject = new JSONObject (s);
                JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                if (NewDataSet.get ("Table") instanceof JSONObject) {
                    JSONObject table = NewDataSet.getJSONObject ("Table");
                    data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                    values.add (data);
                } else if (NewDataSet.get ("Table") instanceof JSONArray) {
                    JSONArray tableArray = NewDataSet.getJSONArray ("Table");

                    for (int i = 0; i < tableArray.length (); i++) {
                        JSONObject table = tableArray.getJSONObject (i);
                        data = new SearchModel (table.getString ("Job_Category"), table.getString ("Min_Exp"), table.getString ("Max_Exp"), table.getString ("Posted_On"), table.getString ("Candidate_Counts"), table.getString ("Applications"), table.getString ("No_Of_Pos"), table.getString ("Job_Desc"), table.getString ("Job_Type"), table.getString ("Job_Hours"), table.getString ("Job_Status"), table.getString ("Job_Exp_Date"), table.getString ("Address"), table.getString ("Gender_Name"), table.getString ("Religion_Name"), table.getString ("Exp_Summary"), table.getString ("IJob_Request_ID"), table.getString ("Requestor_Name"));
                        values.add (data);

                    }

                }
                if (values.size () > 0) {
                   list.getList (values);
                } else {

                }

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

        CommonFunctions.showProgress (c, "", false);
        Intent i = new Intent (c, SearchJobListActivity.class);
        c.startActivity (i);
    }

    @Override
    protected String doInBackground(String... s) {
        response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/FindJobForVendor").send ("Vendor_IEntity_Code=" + "34588A34-E969-4723-84FE-E5409B66A5B7" + "&Job_Code=" + "&Job_Category=1" + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=0" + "&Exp_Months_To=0").body ();
        response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
        Log.e ("Search Jobs", "" + response);
        return response;
    }

    public ArrayList<SearchModel> getList() {
        return values;
    }
}

Class where i m using interface

public class SearchJobList extends ListFragment implements SearchJobAsync.getArrayList {
    private View view;
    private ListView lvSearchJobs;
    private ArrayList<SearchModel> data;
    SearchJobCustomList customList;
    SearchJobAsync searchJobAsync;
    private Context c;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate (R.layout.search_job_lists, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated (savedInstanceState);
        c = getActivity ();
        lvSearchJobs = (ListView) getActivity ().findViewById (android.R.id.list);
        data = new ArrayList<SearchModel> ();
        searchJobAsync = new SearchJobAsync (c, this);

    }

    @Override
    public void getList(ArrayList<SearchModel> data) {
        customList = new SearchJobCustomList (c, data);
        setListAdapter (customList);
    }
}

Logcat

 java.lang.NullPointerException
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:83)
            at com.jobs_on_call_fragments.AsyncTasks.SearchJobAsync.onPostExecute(SearchJobAsync.java:25)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

now how to get the arrayList????? i was getting the arrylist by making it static ,bt its not the right way.So please help me


I think you are trying to get value before doInBackground completes its work. Use some boolean static flag in AsyncTask task and update in onPostExecute. Before getting value check that flag. This is a simple workaround. Else you have to use interface.