1

我已经从 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”。我该如何纠正这个?

谢谢!

4

1 回答 1

1

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

networkThread.start();

话虽如此,您最好使用 AsyncTask 来做这种事情。我想一旦你掌握了窍门,你会发现它更容易。

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

于 2012-04-07T07:13:24.870 回答