1

我正在尝试在异步任务中使用 ksoap2 访问 Web 服务。我写了这段代码:

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private final String NAMESPACE = "http://eko.erdem.com";
private final String URL = "http://159.20.89.38:9398/EkoBiletTest/services/EkoClass?wsdl";
private final String SOAP_ACTION = "http://eko.erdem.com/homePage";
private final String METHOD_NAME = "homePage";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hotelNetwork task = new hotelNetwork();
    task.execute(new String[]{NAMESPACE, URL, SOAP_ACTION, METHOD_NAME});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class hotelNetwork extends AsyncTask<String[], Void, String> {



    @Override
    protected String doInBackground(String[]... params) {

        String[] data = params[0];
        String namespace = data[0];
        String url = data[1];
        String action = data[2];
        String method = data[3];

        String result = "";
        SoapObject request = new SoapObject(namespace, method); 

        String place = "Paris";
        String checkinDate = "2013-06-10";
        String checkoutDate = "2013-06-12";

      //Pass value for place variable of the web service
        PropertyInfo placeProp =new PropertyInfo();
        placeProp.setName("place");//Define the variable name in the web service method
        placeProp.setValue(place);//Define value for place variable
        placeProp.setType(String.class);//Define the type of the variable
        request.addProperty(placeProp);//Pass properties to the variable

      //Pass value for checkinDate variable of the web service
        PropertyInfo checkinDateProp =new PropertyInfo();
        checkinDateProp.setName("checkinDate");
        checkinDateProp.setValue(checkinDate);
        checkinDateProp.setType(String.class);
        request.addProperty(checkinDateProp);


      //Pass value for checkinDate variable of the web service
        PropertyInfo checkoutDateProp =new PropertyInfo();
        checkoutDateProp.setName("checkoutDate");
        checkoutDateProp.setValue(checkoutDate);
        checkoutDateProp.setType(String.class);
        request.addProperty(checkoutDateProp);



        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(url);

        try {
            androidHttpTransport.call(action, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            result = response.toString();



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

    @Override
    protected void onPostExecute(String result){

        TextView tv = (TextView) MainActivity.this.findViewById(R.id.textView);
        tv.setText(result);
        MainActivity.this.setContentView(tv);
    }




}
}

当我运行应用程序而不是来自 Web 服务的响应时,textView 仍显示默认文本“Hello World”。我对其进行了调试,发现 doinBackground 方法实际上得到了响应并返回它,但没有调用 onPostExecute。这可能是什么原因?我听说这可能是由 doinBackground 方法中的异常引起的,但它没有显示异常。任何帮助,将不胜感激。

4

1 回答 1

2

基本上删除MainActivity.this.setContentView(tv);你正在做的事情是在你已经设置新文本之后再次创建默认视图,上面写着“Hello World”

于 2013-06-06T20:32:14.397 回答