在Android上调用Web服务

时间:2014-02-20 06:17:58

标签: android android-asynctask ksoap2

尝试使用Async,但即使没有吐司,也无法处理, 也试过调用方法runUIThread,但没有运气,

异步方法有效,但也会出现运行时异常

当我使用Async方法时,数据在Textview上得到的时间太晚(5-10分钟),但是当我在调试器中使用它时会立即获得,

web服务下面的

是使用wizdl应用程序进行测试,响应是即时的

我很困惑请帮忙

private final String NAMESPACE = "http://????????.com/EventGetter/";

private final String URL = "http://?????????.com/EventGetter.asmx";

private final String SOAP_ACTION = "http://????????.com/EventGetter/GetTimeTable";

private final String METHOD_NAME = "GetTimeTable"; 

private class TPaccessData extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... params) {
        GetData();
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {

        Log.i(LogStr, "onPostExecute");
    }

    @Override
    protected void onPreExecute() {
        GetData();
        Log.i(LogStr, "onPreExecute");

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(LogStr, "onProgressUpdate");
    }
}

public void GetData(){
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //set properties aka param
    request.addProperty("stream","where ttBatch = 'F.Y.J.C'");
    //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(URL);
    try {
        androidHttpTransport.debug = true;
        //Invole web service
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        //Assign it to Text static variable
        text.setText(response.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

还尝试在oncreate方法中调用get数据

 NotifyList.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            GetData();
          //  TPaccessData task = new TPaccessData();
          //   task.execute();
        }
    });

1 个答案:

答案 0 :(得分:2)

从onPreExecute()中删除你的GetData()方法 并在onPost中调用此行

text.setText(response.toString());
相关问题