使用接口方法时获取null

时间:2014-06-27 11:54:33

标签: java android interface android-asynctask

我创建了一个执行所有后台任务的异步任务。在异步任务类中,我已经创建了一个方法完成的接口。它用于让我知道后台任务已经结束。但是当我使用时那个界面我得到它

异步类

 public static class SearchJobsAsync extends AsyncTask<String, String, String> {
            String response;
            boolean value;
            Context c;
            finished finished;

            public SearchJobsAsync(Context c, SearchJobsAsync.finished finished,boolean value) {
                this.c = c;
                this.finished = finished;
                this.value=value;

            }

            public static interface finished {
                void Finish(boolean finish);
            }

            SearchJobsAsync(Context c, boolean value) {
                this.value = value;
                this.c = c;


            }

            @Override
            protected String doInBackground(String... strings) {
                if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0 && spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=99" + "&Exp_Months_To=99").body ();
                } else if (spYears.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=0" + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spToYears.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=25" + "&Exp_Months_To=" + strToMnths).body ();
                } else if (spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=25").body ();
                } else if (spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=25" + "&Exp_Months_To=25").body ();
                } else if (spYears.getSelectedItemPosition () == 0 && spMonths.getSelectedItemPosition () == 0 && spToYears.getSelectedItemPosition () == 0 && spToMonths.getSelectedItemPosition () == 0) {
                    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=" + strCategory + "&Exp_Years_From=0" + "&Exp_Months_From=0" + "&Exp_Years_To=25" + "&Exp_Months_To=25").body ();
                } else {
                    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=" + strCategory + "&Exp_Years_From=" + strYrs + "&Exp_Months_From=" + strMnths + "&Exp_Years_To=" + strToYrs + "&Exp_Months_To=" + strToMnths).body ();

                }
                //Vendor_IEntity_Code=string&Job_Code=string&Job_Category=string&Exp_Years_From=string&Exp_Months_From=string&Exp_Years_To=string&Exp_Months_To=string
                response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
                Log.e (" Search Job Response", "" + response);
                return response;

            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute ();
    //            CommonFunctions cs = new CommonFunctions ();
    //            cs.showProgressBar (c, true);
                pDialog = new ProgressDialog (c);
                pDialog.setMessage ("Please wait...");
                pDialog.setCancelable (false);
                pDialog.show ();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute (s);
                arraylist = new ArrayList<HashMap<String, String>> ();
                try {
                    jsonObject = new JSONObject (s);
                    NewDataSet = jsonObject.getJSONObject ("NewDataSet");
                    if (NewDataSet == null) {
                        Toast.makeText (c, "error", Toast.LENGTH_SHORT).show ();
                    } else if (NewDataSet.get ("Table") instanceof JSONObject) {
                        HashMap<String, String> map = new HashMap<String, String> ();
                        JSONObject table = NewDataSet.getJSONObject ("Table");

                        map.put ("category", table.getString ("Job_Category"));
                        map.put ("minExp", table.getString ("Min_Exp"));
                        map.put ("maxExp", table.getString ("Max_Exp"));
                        map.put ("postedOn", table.getString ("Posted_On"));
                        map.put ("counts", table.getString ("Candidate_Counts"));
                        map.put ("applied", table.getString ("Applications"));
                        map.put ("position", table.getString ("No_Of_Pos"));
                        map.put ("desc", table.getString ("Job_Desc"));
                        map.put ("type", table.getString ("Job_Type"));
                        map.put ("hours", table.getString ("Job_Hours"));
                        map.put ("status", table.getString ("Job_Status"));
                        map.put ("expiryDate", table.getString ("Job_Exp_Date"));
                        map.put ("address", table.getString ("Address"));
                        map.put ("state", table.getString ("Job_State"));
                        map.put ("country", table.getString ("Job_Country"));
                        map.put ("city", table.getString ("Job_City"));
                        map.put ("gender", table.getString ("Gender_Name"));
                        map.put ("religion", table.getString ("Religion_Name"));
                        map.put ("summary", table.getString ("Exp_Summary"));
                        map.put ("requestId", table.getString ("IJob_Request_ID"));
                        map.put ("requestorName", table.getString ("Requestor_Name"));
    //                    map.put ("requestId",table.getString ("IJob_Request_ID"));


                        arraylist.add (map);


                    } else if (NewDataSet.get ("Table") instanceof JSONArray) {

                        JSONArray tablearray = NewDataSet.getJSONArray ("Table");

                        for (int i = 0; i < tablearray.length (); i++) {
                            HashMap<String, String> map = new HashMap<String, String> ();

                            JSONObject table = tablearray.getJSONObject (i);

                            map.put ("code", table.getString ("Job_Code"));
                            map.put ("category", table.getString ("Job_Category"));
                            map.put ("minExp", table.getString ("Min_Exp"));
                            map.put ("maxExp", table.getString ("Max_Exp"));
                            map.put ("postedOn", table.getString ("Posted_On"));
                            map.put ("counts", table.getString ("Candidate_Counts"));
                            map.put ("applied", table.getString ("Applications"));
                            map.put ("position", table.getString ("No_Of_Pos"));
                            map.put ("desc", table.getString ("Job_Desc"));
                            map.put ("type", table.getString ("Job_Type"));
                            map.put ("hours", table.getString ("Job_Hours"));
                            map.put ("status", table.getString ("Job_Status"));
                            map.put ("expiryDate", table.getString ("Job_Exp_Date"));
                            map.put ("address", table.getString ("Address"));
                            map.put ("state", table.getString ("Job_State"));
                            map.put ("country", table.getString ("Job_Country"));
                            map.put ("city", table.getString ("Job_City"));
                            map.put ("gender", table.getString ("Gender_Name"));
                            map.put ("religion", table.getString ("Religion_Name"));
                            map.put ("summary", table.getString ("Exp_Summary"));
                            map.put ("requestId", table.getString ("IJob_Request_ID"));
                            map.put ("requestorName", table.getString ("Requestor_Name"));


                            arraylist.add (map);
    //                        add.add (values);


                        }


                    }
                } catch (JSONException e) {
                    e.printStackTrace ();
                }
                if (value) {
                    finished.Finish (true);
                    Intent i = new Intent (c, SearchJobsList.class);
                    c.startActivity (i);
                }
                if (pDialog.isShowing ()) {
                    pDialog.dismiss ();
                }

            }
        }

        @Override
        public void onBackPressed() {
            super.onBackPressed ();
            Intent i = new Intent (c, Home.class);
            startActivity (i);
        }
    }

logcat的

06-27 17:14:30.406  11554-11554/com.example.jobs_on_call.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.jobs_on_call_search_jobs.SearchJobs$SearchJobsAsync.onPostExecute(SearchJobs.java:334)
            at com.example.jobs_on_call_search_jobs.SearchJobs$SearchJobsAsync.onPostExecute(SearchJobs.java:160)
            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)

1 个答案:

答案 0 :(得分:0)

//尝试以下代码。只做一个构造函数。

//接口代码是:

 public interface WebServiceListener {

 // what ever you want to use arraylist or map.
 public void onActionComplete (ArrayList<String> _arraList);     
 public void onActionCompletes (HashMap<String, String> _map);   
 }

//你的异步类

 public class SearchJobsAsync extends AsyncTask<String, String, String> {

    WebServiceListener _object = null;
    Context _CTX;


  public SearchJobsAsync(Context _ctx,WebServiceListener object) {
    this._CTX = _ctx;
    this._object = object;
}

@Override
protected String doInBackground(String... params) {
    String _data = null;
    try {
        // do your stuff
    } catch (Exception e) {
        e.printStackTrace();
    }

    return _data;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);    
    HashMap<String, String> map = new HashMap<String,String>();
    map.put("KEY", result);
    object.onActionCompletes(map);
    _arraylist.add(result);
    object.onActionComplete(_arraylist);
}

}

//现在在你想要数据的yiour主活动中,只需创建Async类的对象并传递如下参数:

  SearchJobsAsync   _parse = new SearchJobsAsync(_ctx,this);

//现在补充接口的方法并获取如下数据:

@Override
public void onActionComplete(ArrayList<String> _array) {
    String _mydata = _array.get(0);
    System.out.println("hhhh" + _mydata);

}

@Override
public void onActionCompletes(HashMap<String, String> _map) {
    String _mydatas = _map.get("pankaj");
    System.out.println("thakur shab" + _mydatas);

}