【问题标题】:Passing String Array from AsyncTask OnpostExecute to Mainactivity Not Working将字符串数组从 AsyncTask OnpostExecute 传递给 Mainactivity 不起作用
【发布时间】:2015-07-09 08:29:14
【问题描述】:

我从Mainactivity 呼叫Asynctask 每5 分钟使用一次警报管理器

我的Asynctask 保存在新的服务类中。

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

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

问题:

我想将 String Array value 从 Web 服务 doInBackground 传递给 Mainactivity is Getting 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

【问题讨论】:

  • 请插入代码...
  • 我添加了我的代码@B.Kemmer

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


【解决方案1】:

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

例如

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

实现如下活动接口

public class MainActivity extends Activity implements WebCallable

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

例如

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

在 onPostExecute 写入

context.response(STRING_ARRAY)

更好的是,从 doInBackground 返回字符串响应并将其作为 bekow 进入 onPostExecute

protected void onPostExecute(String result) 

并在 onPostExecute 中创建数组。

或创建 STRING_ARRAY 对象类级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多