2013-09-25 164 views
0

我是android開發中的新手。我得到了SOAP響應,但我不知道如何解析SOAP響應的值。 其實我試圖從響應中解析String「Emotion」。 請找我的XML文檔和響應這裏http://letsmote.com:8087/EmoteSAPI.asmx?op=GetEmotionList 這裏是我的代碼如何解析SOAP響應

public class EmotionList extends Activity { 

    public final String NAMESPACE = "http://tempuri.org"; 
    public final String URL = "http://letsmote.com:8087/EmoteSAPI.asmx"; 
    public final String SOAP_ACTION = "http://tempuri.org/GetEmotionList"; 

    public final String METHOD_NAME = "GetEmotionList"; 

    //SharedPreferences sp; 
    String[] Emotionlist; 
    EditText edt; 

    Button GetEmotion; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_emotion_list); 

      // TODO Auto-generated method stub 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 



       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

       Element[] header = new Element[1]; 
       header[0] = new Element().createElement("http://tempuri.org/","SecurityInfo"); 
       Element APIkey = new Element().createElement(null,"n0:APIKey"); 
       APIkey.addChild(Node.TEXT,"BcodTestAPI"); 
       header[0].addChild(Node.ELEMENT,APIkey); 

       Element user = new Element().createElement(null,"n0:UserID"); 
       user.addChild(Node.TEXT,"bcod"); 
       header[0].addChild(Node.ELEMENT,user); 

       Element passid = new Element().createElement(null,"n0:Password"); 
       passid.addChild(Node.TEXT, "bcod"); 
       header[0].addChild(Node.ELEMENT, passid); 


       // add header to envelope 
       envelope.headerOut = header; 

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

       try { 
         androidHttpTransport.call(SOAP_ACTION, envelope); 
         //SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
         Log.e("myApp",envelope.getResponse().toString()); 

         SoapObject response = (SoapObject) envelope.getResponse(); 
         Log.e(" ","----------Response----------"+response.toString()); 
         System.out.println("===Response==="); 
         SoapObject CinemaCode; 
         SoapObject pii = (SoapObject)response.getAttribute("Emotion"); 
         Log.e(" ","----------Response1----------"+response.toString()); 
         CinemaCode = (SoapObject)pii.getProperty(1); 
         String []categories = new String [CinemaCode.getPropertyCount()]; 

         String []Emotions = new String[1000]; 

         for (int i = 0; i < categories.length; i++) 
         { 
         SoapObject list = (SoapObject)CinemaCode.getProperty(i);    
         Emotions[i] = list.getProperty(2).toString(); 
         Log.e("","Emotions Are: = "+Emotions[i]); 

         } 
       }catch(Exception e) 
       { 

       } 

      } 

這是我得到的迴應....

十一月9日至25日:38:20.357:E /( 626):----------迴應---------- anyType {Emotions = anyType {Emotion = Joy;顏色=#9370DB; EmotionId = 17; IsHappyEmotion = TRUE; };情緒= {anyType的情感=自由;顏色=#800080; EmotionId = 12; IsHappyEmotion = TRUE; };情緒= {anyType的情感=洛夫;顏色=#8B008B; EmotionId = 19; IsHappyEmotion = TRUE; };情緒= {anyType的情感=激情;顏色=#008080; EmotionId = 21; IsHappyEmotion = TRUE; };情緒= {anyType的情感=積極性;顏色=#B8860B; EmotionId = 10; IsHappyEmotion = TRUE; };情緒= {anyType的情感=樂觀;顏色=#696969; EmotionId = 20; IsHappyEmotion = TRUE; };情緒= {anyType的情感=樂觀;顏色=#98F5FF; EmotionId = 15; IsHappyEmotion = TRUE; };情緒= {anyType的情感=知足;顏色=#0000FF; EmotionId = 4; IsHappyEmotion = TRUE; };情緒= {anyType的情感=無聊;顏色=#8A2BE2; EmotionId = 3; IsHappyEmotion = FALSE; };情緒= {anyType的情感=悲觀;顏色=#48D1CC; EmotionId = 22; IsHappyEmotion = TRUE; };情緒= {anyType的情感=挫折;顏色=#FFC0CB; EmotionId = 24; IsHappyEmotion = FALSE; };情緒= {anyType的情感=失望;顏色=#D2691E; EmotionId = 7; IsHappyEmotion = FALSE; };情緒= {anyType的情感=懷疑;顏色=#CAFF70; EmotionId = 9; IsHappyEmotion = FALSE; };情緒= {anyType的情感=擔心;顏色=#008000; EmotionId = 23; IsHappyEmotion = FALSE; };情緒= {anyType的情感=責備;顏色=#7CFC00; EmotionId = 2; IsHappyEmotion = FALSE; };情緒= {anyType的情感=氣餒;顏色=#ADFF2F; EmotionId = 8; IsHappyEmotion = FALSE; };情緒= {anyType的情感=嫉妒;顏色=#006400; EmotionId = 16; IsHappyEmotion = FALSE; };情緒= {anyType的情感=內疚;顏色=#FFFF00; EmotionId = 13; IsHappyEmotion = FALSE; };情緒= {anyType的情感=孤獨;顏色=#BDB76B; EmotionId = 18; IsHappyEmotion = FALSE; };情緒= {anyType的情感=恐懼;顏色=#FF8C00; EmotionId = 11; IsHappyEmotion = FALSE; };情緒= {anyType的情感=憤怒;顏色=#FF0000; EmotionId = 1; IsHappyEmotion = FALSE; };情緒= {anyType的情緒抑鬱=;顏色=#8B0000; EmotionId = 5; IsHappyEmotion = FALSE; };情緒= {anyType的情感=絕望;顏色=#CD5C5C; EmotionId = 6; IsHappyEmotion = FALSE; };情緒= {anyType的情感=恨;顏色=#800000; EmotionId = 14; IsHappyEmotion = FALSE; }; }

由於提前

回答

1

試試這個方法,並檢查是否正常工作

public void Parser(){ 

     class TopGoalScores { 
      String Emotion; 
      int Color; 
      String EmotionId; 
      String IsHappyEmotion; 

     } 

     TopGoalScores topGoalScores=new TopGoalScores(); 

     try { 
      SoapParser.parseBusinessObject(response.getProperty(0).toString(), topGoalScores); 

     } catch (NumberFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 

這是SOAP語法分析器類

import java.lang.reflect.Field; 
import java.lang.reflect.Type; 

public class SoapParser { 

    /** 
    * Parses a single business object containing primitive types from the 
    * response 
    * 
    * @param input 
    *   soap message, one element at a time 
    * @param theClass 
    *   your class object, that contains the same member names and 
    *   types for the response soap object 
    * @return the values parsed 
    * @throws NumberFormatException 
    * @throws IllegalArgumentException 
    * @throws IllegalAccessException 
    * @throws InstantiationException 
    */ 
    public static void parseBusinessObject(String input, Object output) 
      throws NumberFormatException, IllegalArgumentException, 
      IllegalAccessException, InstantiationException { 

     Class theClass = output.getClass(); 
     Field[] fields = theClass.getDeclaredFields(); 

     for (int i = 0; i < fields.length; i++) { 
      Type type = fields[i].getType(); 
      fields[i].setAccessible(true); 

      // detect String 
      if (fields[i].getType().equals(String.class)) { 
       String tag = "s" + fields[i].getName() + "="; // "s" is for 
                   // String in the 
                   // above soap 
                   // response 
                   // example + 
                   // field name 
                   // for example 
                   // Name = 
                   // "sName" 
       if (input.contains(tag)) { 
        String strValue = input.substring(
          input.indexOf(tag) + tag.length(), 
          input.indexOf(";", input.indexOf(tag))); 
        if (strValue.length() != 0) { 
         fields[i].set(output, strValue); 
        } 
       } 
      } 

      // detect int or Integer 
      if (type.equals(Integer.TYPE) || type.equals(Integer.class)) { 
       String tag = "i" + fields[i].getName() + "="; // "i" is for 
                   // Integer or 
                   // int in the 
                   // above soap 
                   // response 
                   // example+ 
                   // field name 
                   // for example 
                   // Goals = 
                   // "iGoals" 
       if (input.contains(tag)) { 
        String strValue = input.substring(
          input.indexOf(tag) + tag.length(), 
          input.indexOf(";", input.indexOf(tag))); 
        if (strValue.length() != 0) { 
         fields[i].setInt(output, Integer.valueOf(strValue)); 
        } 
       } 
      } 

      // detect float or Float 
      if (type.equals(Float.TYPE) || type.equals(Float.class)) { 
       String tag = "f" + fields[i].getName() + "="; 
       if (input.contains(tag)) { 
        String strValue = input.substring(
          input.indexOf(tag) + tag.length(), 
          input.indexOf(";", input.indexOf(tag))); 
        if (strValue.length() != 0) { 
         fields[i].setFloat(output, Float.valueOf(strValue)); 
        } 
       } 
      } 
     } 

    } 
} 

還與源代碼教程

看到這個link

+0

你做完了嗎? –

1

試試這個代碼片段

public static Category[] RetrieveFromSoap(SoapObject soap) 
    { 
     Category[] categories = new Category[soap.getPropertyCount()]; 
     for (int i = 0; i < categories.length; i++) { 
      SoapObject pii = (SoapObject)soap.getProperty(i); 
      Category category = new Category(); 
      category.CategoryId = Integer.parseInt(pii.getProperty(0).toString()); 
      category.Name = pii.getProperty(1).toString(); 
      category.Description = pii.getProperty(2).toString(); 
      categories[i] = category; 
     } 
     return categories; 
    } 
+0

什麼是分類[這裏]?它是構造函數嗎? – Gauraw

+0

@Gauraw它是類**的數組**類是類。 –