从AsyncTask OnpostExecute传递字符串数组到Mainactivity不工作

时间:2015-07-09 08:29:15

标签: java android arrays web-services android-asynctask

我从Asynctask拨打Mainactivity每隔5分钟使用一次警报管理器

我的Asynctask保存在新服务类

使用Asynctask我正在从Web服务中检索字符串值。

String Array中保存的网络服务中检索工作正常。

问题:

我希望将字符串数组值从Web服务doInBackground传递给 Mainactivity is Null

帮助我如何实现这一目标。

link i refered,但没有成功

 private class AsyncCallWSfor_SERVICE extends AsyncTask<String, Void, Void>
        {
            @Override
            protected Void doInBackground(String... params) 
            {
                Log.i(TAG1, "doInBackground");
                try 
                {

                    getdatafromWeb();

                } 
                catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(),
                            "error caught in do in background", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();

                }
                return null;

            }

            @Override
            protected void onPreExecute()
            { 
            /*  //progress.setTitle("Progress");
                progress.setMessage("Please Wait Loading Data...");
                progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progress.setIndeterminate(true);
                progress.show();*/




                Log.i(TAG1, "onPreExecute");

            }
            @Override
            protected void onPostExecute(Void result)
            {


                // To dismiss the dialog
            //    progress.dismiss();

                Log.i(TAG1, "onPostExecute");

            }

        }


        public void getdatafromWeb() {
            // Create request
            SoapObject request = new SoapObject(NAMESPACE1, METHOD_NAME1);

            // Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            // Set output SOAP object
            envelope.setOutputSoapObject(request);
            // Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);

            try {
                // Invole web service
                androidHttpTransport.call(SOAP_ACTION1, envelope);
                // Get the response
                SoapPrimitive response1 = (SoapPrimitive) envelope.getResponse();


                //Converting string to Array list
                  ArrayList<String> KitDistributedString_array= new ArrayList<String>();



                if ((response1.toString()).contains("{")) {

                    SoapObject rep = (SoapObject) envelope.bodyIn;
                    JSONArray jr = new JSONArray(rep.getPropertyAsString(0)
                            .toString());
                    for (int i = 0; i < jr.length(); i++) {
                        JSONObject jb = (JSONObject) jr.get(i);



                        KitValueString = jb.getString("KitValue");


                        Log.d(TAG1 +"MY Data" , KitValueString);

                        KitDistributedString_array.add(KitValueString);


                    }


    STRING_ARRAY = new String[KitDistributedString_array.size()];
    STRING_ARRAY = KitDistributedString_array.toArray(STRING_ARRAY);



                } 
                else 
                {
                    Status_Response_Service = response1.toString();
                }

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

        }

我想将此 STRING_ARRAY 传递给MainActivity

1 个答案:

答案 0 :(得分:1)

为了获得对调用者活动的响应,我建议编写一个你需要为调用者活动实现的接口

例如

public interface WebCallable {
    public void response(String[] result);
}

实现活动界面,如下所示

public class MainActivity extends Activity implements WebCallable

现在从AsyncTask<String, Void, String[]>扩展服务类,并通过&#39; this&#39;将活动对象作为参数传递。您可以在类级别保存此对象。

e.g。

public AsyncCallWSfor_SERVICE(Context context){
   this.context = context
}

在onPostExecute中写

context.response(STRING_ARRAY)

更好,从doInBackground返回字符串响应并将其作为bekow以onPostExecute获取

protected void onPostExecute(String result) 

并在onPostExecute中创建数组。

或者创建STRING_ARRAY对象类级别。