如何在android中解析和显示WCF的内容?

时间:2011-09-06 07:54:27

标签: android wcf xml-parsing

使用网络服务。 我正在做POC。 这是我的活动课: 我正在使用kso​​ap2。

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());
        }

    }
}

我得到的结果如下:

<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>

请告诉我如何解析它。我应该使用哪个类,我应该使用哪种方法?

1 个答案:

答案 0 :(得分: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 

享受快乐编码