-2

I am Calling a Asynctask from Mainactivity Using alarm manager Every 5 minutes

My Asynctask is kept in a New Service Class.

Using that Asynctask I'm retrieving String values from Web service.

Retrieving from web Service Saving in String Array Works fine.

Problem:

I want pass that String Array value from Web service doInBackground to Mainactivity is Getting Null.

Help me how to achieve this.

link i refered ,but no success

 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();
            }

        }

I want to pass this STRING_ARRAY to MainActivity

Community
  • 1
  • 1
Kumar
  • 969
  • 2
  • 22
  • 56

1 Answers1

1

For getting the response to the caller activity, I suggest to write a interface Which you need to implement to caller activity

e.g

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

implement interface to activity as below

public class MainActivity extends Activity implements WebCallable

Now extend service class from AsyncTask<String, Void, String[]> and pass activity object as paramenter by 'this'. You can save this object at class level.

e.g.

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

In onPostExecute write

context.response(STRING_ARRAY)

Better, return string response from doInBackground and get it in onPostExecute as bekow

protected void onPostExecute(String result) 

and create array in onPostExecute.

Or cretae STRING_ARRAY object class level.

compyutech
  • 578
  • 3
  • 8
  • 26