2012-04-07 233 views
1

我已經訪問了Android 2.2的Web服務。它完全OK。我將Android 4.0.3的程序更改爲訪問相同的Web服務。但這不起作用。 我的Android程序代碼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

它看起來像你從未開始networkThread。您需要這在你的onCreate()的末尾:

networkThread.start(); 

話雖這麼說,你會很好地使用的AsyncTask做這類的事情。我認爲一旦你掌握了它,你會發現它更容易。

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

+0

Thanx!它解決了我的問題.. – Grant 2012-04-07 07:21:11

+0

很高興我能幫上忙。 :) – 2012-04-07 08:04:30