0

我有 asynctask 类,我在其中执行使用 web 服务并解析响应。解析后我得到列表<>。我怎样才能通过我的其他班级?

这是我的异步任务类

String response;
    List<Item> lstresponse_locations = null;
//  CustomAdapter CustAdapter;
//  List<Item> lstresult = new ArrayList<Item>();
//  ListView lstcities;



//  String params;
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub\

        SendRequesttoServer(params);
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

// getting response from webservice

    public void SendRequesttoServer(String[] params) {

        try {

        SOAP_ACTION = NAMESPACE + METHOD;
        SoapObject request = new SoapObject(NAMESPACE, METHOD);
        request.addProperty("CityName", params[0]);

        SoapSerializationEnvelope res = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
        res.dotNet = true;
        res.setOutputSoapObject(request);

        HttpTransportSE call = new HttpTransportSE(url);

        SoapPrimitive result;
        call.call(SOAP_ACTION, res);
        result = (SoapPrimitive) res.getResponse();
        ParseLocations Objparselocations = new ParseLocations(
                    new ByteArrayInputStream(result.toString()
                            .getBytes("UTF-8")));
        lstresponse_locations = Objparselocations.parse();

        response = lstresponse_locations.toString();
            // lstCities.toArray();

        System.out.println(lstresponse);
    } catch (SoapFault e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
}

我怎样才能通过响应?谢谢 ramu

4

3 回答 3

0

Try to override the onPostExecute in you activity itself and create a adapter object inside of the onPostExecute and set the adapter to your listview like

Class MyActivity extends Activity {

    public void onCreate(Bundle args) {
     ......
     ......
     new MyAsyncTask() {

       public void onPostExecute(List<Object> list) {
          listView.setAdapter(new MyAdapter(list));
       }

     }.execute(params);
     .......
     ........
   }
}

Edit : In your AsyncTask class make the third parameter as List like

class MyAsync extends AsycnTask<String, Integer, List<Object>>
于 2013-03-08T10:59:30.853 回答
0

just pass your ListView and its Adapter to the AsyncTask in the constructor, set the Adapter in the onPostExecute() method. Worked fine for me

于 2013-03-08T11:00:08.260 回答
0

字符串由 doInBackground 返回以发布执行,因此 postExceute 中的结果是您的响应

  protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
Log.d("Response: ",result);

}
于 2013-03-08T10:30:29.927 回答