Android Web服务访问

时间:2012-04-07 06:37:51

标签: java android web-services axis2 ksoap2

我已经从Android 2.2访问了一个Web服务。它非常好。我改变了我的Android 4.0.3程序以访问相同的Web服务。但这不起作用。 我的Android程序代码

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

 import android.widget.TextView;
 import android.app.Activity;
 import android.os.Bundle;

 public class AndroidWSClientActivity extends Activity {

private static final String SOAP_ACTION = "http://ws.android4.com/";
private static final String METHOD_NAME = "sayHello";
private static final String NAMESPACE = "http://ws.android4.com/sayHello/";
private static final String URL = "http://175.157.45.91:8085/ForAndroid4/services/TestWs?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Thread networkThread = new Thread() {
    @Override
    public void run() {
       try {
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);

         HttpTransportSE ht = new HttpTransportSE(URL);
         ht.call(SOAP_ACTION, envelope);
         final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
         final String str = response.toString();

         runOnUiThread (new Runnable(){

          public void run() {
              TextView result;
               result = (TextView)findViewById(R.id.textView1);
               result.setText(str);
              }
           }

          } 

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

我也添加了互联网许可。 这个程序正确安装在模拟器上。但没有显示结果。没有任何错误显示.Eclipse说没有使用“networkThread”。我怎么能纠正这个?

谢谢!

1 个答案:

答案 0 :(得分:1)

看起来你从未启动过networkThread。你需要在onCreate()的最后:

networkThread.start();

话虽如此,你最好使用AsyncTask来做这类事情。我认为一旦掌握了它,你会发现它会更容易。

http://developer.android.com/reference/android/os/AsyncTask.html