asynctask不执行onPostExecute()

时间:2013-06-06 20:09:01

标签: java android eclipse android-asynctask android-ksoap2

我正在尝试使用assectask中的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方法中的异常引起的,但它没有显示异常。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

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