2011-09-06 67 views
1

您好,使用網絡服務。 我正在做一個POC。 這是我的活動課: 我使用kso​​ap2。如何在android中解析和顯示WCF的內容?

package ab.tina; 

import java.io.InputStream; 
import java.net.URI; 

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.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class TinaWebActivity extends Activity { 

    private TextView txtview; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     txtview=(TextView)findViewById(R.id.textView1); 
     try { 
      SoapObject Request = new SoapObject("http://www.webserviceX.NET/", "GetQuote"); 
      Request.addProperty("symbol", "MFST"); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(Request); 


      HttpTransportSE androidHttpTransport = new HttpTransportSE("http://www.webservicex.net/stockquote.asmx"); 


      Log.e("tag1","err1"); 
      androidHttpTransport.call("http://www.webserviceX.NET/GetQuote", envelope); 
      Log.e("tag2","err2"); 

      SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
      Log.e("tag3","err3"); 
      //String str=(String)response.getProperty(1); 
      txtview.setText(response.toString()); 
      //txtview.setText(response.toString()); 
      Log.e("tag4","err4"); 
     } 
     catch (Exception e) { 
       Log.e("tag5","err5"); 
      txtview.setText(e.getMessage()); 
     } 

    } 
} 

我m如果結果是這樣的:

<string xmlns="http://www.webserviceX.NET/"> 
<StockQuotes><Stock><Symbol>MFST</Symbol><Last>0.00</Last><Date>N/A</Date><Time>N/A</Time><Change>N/A</Change><Open>N/A</Open><High>N/A</High><Low>N/A</Low><Volume>N/A</Volume><MktCap>N/A</MktCap><PreviousClose>N/A</PreviousClose><PercentageChange>N/A</PercentageChange><AnnRange>N/A - N/A</AnnRange><Earns>N/A</Earns><P-E>N/A</P-E><Name>MFST</Name></Stock></StockQuotes> 
</string> 

請告訴我如何可以解析它。我將需要哪些課程以及我應該使用哪種方法?

+0

檢查這些了,可能有所幫助:http://www.kevingao.net/wcf-java-interop和http ://www.kevingao.net/wcf-java-interop/java-client-and-wcf-server.html或http://java.sun.com/developer/technicalArticles/glassfish/ProjectTango/ - http:// social.msdn.microsoft.com/forums/en-US/wcf/thread/4c5d88fb-5427-4024-aef1-94b601745dd1/ – bkdc

回答

0

好吧,如果你想從你需要搜索soapobject裏面的信息,每個節點都是一個soapobject。

你的情況

你做這樣的事情:

resultData = (SoapObject)resultData.resultData.getProperty("StockQuotes"); 
//now resultData have all node of stockQuotes 
resultData = (SoapObject)resultData.resultData.getProperty("Stock"); 
//now resultdata have stock 

//now to parse string saftelly go to ksoap 2 android project download last version 
String symbol = resulresultData.getPropertySafelyAsString("Symbol", "NO DATA"); 
//or 
String symbol = resulresultData.getPropertySafelyAsString("Symbol"); 

//and so on for other XML 

玩得開心快樂編碼

+0

如果你不能這樣做告訴我我會很樂意幫助 – PedroAGSantos

+0

http:// seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html – PedroAGSantos

相關問題