2013-08-06 98 views
0

我已經使用Ksoap2連接到這個.NET Web服務,當我輸入用戶ID時,我得到一個xml響應。我只想看到兩個標籤callTitle和callDescription。我不需要其餘部分,並且希望在文本中看到沒有用xml代碼包圍的文字。有人可以幫忙嗎?我無法在線找到教程。Ksoap2解析器xml數據

public class AndroidWebService extends Activity { 

    /** Called when the activity is first created. */ 
    private static String SOAP_ACTION = "http://tempuri.org/GetHelpDeskCalls"; 
    private static String NAMESPACE = "http://tempuri.org/"; 
    private static String METHOD_NAME = "GetHelpDeskCalls"; 
    static final String URL = "https:/192.2344.123:8080/Service1.asmx"; 

    Button getData; 
    EditText userID; 
    TextView data; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.helpdesk); 

     getData = (Button) findViewById(R.id.button1); 
     userID = (EditText) findViewById(R.id.txtFar); 
     data = (TextView) findViewById(R.id.textView1); 

     Thread nT = new Thread() { 
      @Override 
      public void run() { 

       getData.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 

         SoapObject request = new SoapObject(NAMESPACE, 
           METHOD_NAME); 

         request.addProperty("userID", userID.getText() 
           .toString()); 




         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
           SoapEnvelope.VER11); 

         envelope.setOutputSoapObject(request); 
         envelope.dotNet = true; 
         HttpTransportSE androidHttpTransport = new HttpTransportSE(
           URL); 

         try { 

          androidHttpTransport.debug = true; 
          androidHttpTransport.call(SOAP_ACTION, envelope); 

          final String results = androidHttpTransport.responseDump 
            .toString(); 

          runOnUiThread(new Runnable() { 
           public void run() { 

            data.setText(results.toString()); 
           } 

          }); 

         } catch (Exception e) { 

          data.setText("Error" + e); 

         } 

        } 

       }); 

      } 

     }; 
     nT.start(); 
    } 



} 

回答

0

你應該分析使用XML拉解析器的幫助你的XML

下面的代碼將引導你這樣做

MyXmlPullParser objMyXmlPullParser = new MyXmlPullParser(context); 
         List<Map<String , String>> list = objMyXmlPullParser.readXml("Xml respose put here", "Table"); 



public class MyXmlPullParser 
{ 
    Context _context ; 

    public MyXmlPullParser(Context _context) 
    { 
     this._context = _context ; 
    } 

    public List<Map<String , String>> readXml(String XmlString , String ParentTag) 
    { 
     Map<String , String > map = new HashMap<String, String>(); 
     List<Map<String , String >> list = new ArrayList<Map<String , String >>(); 

     try 
     { 
      String Tag = "" ; 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(true); 
      XmlPullParser xpp = factory.newPullParser(); 

      xpp.setInput(new StringReader (XmlString)); 
      int eventType = xpp.getEventType(); 

      while (true) 
      { 
       if(eventType == XmlPullParser.START_TAG) 
       { 
        Tag = xpp.getName(); 
       } 
       else if(eventType == XmlPullParser.END_TAG) 
       { 
        Tag = "" ; 
        if(xpp.getName().equals(ParentTag)) 
        { 
         list.add(map); 
         map = new HashMap<String, String>(); 
        } 
       } 
       else if(eventType == XmlPullParser.TEXT) 
       { 
        String text = xpp.getText(); 
        if(!Tag.equals("") && !Tag.equals(ParentTag)) 
        { 
         map.put(Tag, text); 
        } 
       } 
       else if(eventType == XmlPullParser.END_DOCUMENT) 
       { 
        System.out.println("End document"); 
        break ; 
       } 
       eventType = xpp.next(); 
      } 
     } 

     catch (XmlPullParserException e) 
     { 
      Log.e("xml reader" , "error in parsing xml"); 
      return null ; 
     } 
     catch (IOException e) 
     { 
      Log.e("xml reader" , "error in IO in xml"); 
      return null ; 
     } 
     return list ; 
    } 

希望它可以幫助你

+0

做我做一個xml解析的新類然後在Activity中調用它? – user2500733

+0

是的,您可以創建一個新的類來解析數據,並在其構造函數中傳遞參數。 – Andrain

+0

對不起,打擾你我只是盯着android開發,所以我不擅長它可以告訴我如何使用我的代碼上的XML解析器代碼 – user2500733