Java 如何进行SOAP响应的解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18997680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do the Parsing of SOAP Response
提问by Gauraw
I am new in android development.I am getting the SOAP response but I do not know that how to do the parsing of values of the SOAP Response. Actually I am trying to parse the String "Emotion" from the response. Please find my XML document and response here http://letsmote.com:8087/EmoteSAPI.asmx?op=GetEmotionListHere is my code
我是 android 开发的新手。我正在收到 SOAP 响应,但我不知道如何解析 SOAP 响应的值。实际上,我正在尝试从响应中解析字符串“Emotion”。请在这里找到我的 XML 文档和回复http://letsmote.com:8087/EmoteSAPI.asmx?op=GetEmotionList这是我的代码
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)
{
}
}
Here is the response which I am getting....
这是我得到的回应......
09-25 11:38:20.357: E/(626): ----------Response----------anyType{Emotions=anyType{Emotion=Joy; Color=#9370DB; EmotionId=17; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Freedom; Color=#800080; EmotionId=12; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Love; Color=#8B008B; EmotionId=19; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Passion; Color=#008080; EmotionId=21; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Enthusiasm; Color=#B8860B; EmotionId=10; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Optimism; Color=#696969; EmotionId=20; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Hopefulness; Color=#98F5FF; EmotionId=15; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Contentment; Color=#0000FF; EmotionId=4; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Boredom; Color=#8A2BE2; EmotionId=3; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Pessimism; Color=#48D1CC; EmotionId=22; IsHappyEmotion=true; }; Emotions=anyType{Emotion=Frustration; Color=#FFC0CB; EmotionId=24; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Disappointment; Color=#D2691E; EmotionId=7; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Doubt; Color=#CAFF70; EmotionId=9; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Worry; Color=#008000; EmotionId=23; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Blame; Color=#7CFC00; EmotionId=2; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Discouragement; Color=#ADFF2F; EmotionId=8; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Jealousy; Color=#006400; EmotionId=16; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Guilt; Color=#FFFF00; EmotionId=13; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Loneliness; Color=#BDB76B; EmotionId=18; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Fear; Color=#FF8C00; EmotionId=11; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Anger; Color=#FF0000; EmotionId=1; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Depression; Color=#8B0000; EmotionId=5; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Despair; Color=#CD5C5C; EmotionId=6; IsHappyEmotion=false; }; Emotions=anyType{Emotion=Hatred; Color=#800000; EmotionId=14; IsHappyEmotion=false; }; }
09-25 11:38:20.357: E/(626): ----------Response---------anyType{Emotions=anyType{Emotion=Joy; 颜色=#9370DB;EmotionId=17; IsHappyEmotion=真;}; 情绪=任何类型{情绪=自由;颜色=#800080;EmotionId=12; IsHappyEmotion=真;}; 情感=任何类型{情感=爱;颜色=#8B008B;EmotionId=19; IsHappyEmotion=真;}; 情绪=任何类型{情绪=激情;颜色=#008080;EmotionId=21; IsHappyEmotion=真;}; 情绪=任何类型{情绪=热情;颜色=#B8860B;EmotionId=10; IsHappyEmotion=真;}; 情绪=任何类型{情绪=乐观;颜色=#696969;EmotionId=20; IsHappyEmotion=真;}; 情绪=任何类型{情绪=希望;颜色=#98F5FF;EmotionId=15; IsHappyEmotion=真;}; 情绪=任何类型{情绪=满足;颜色=#0000FF;EmotionId=4; IsHappyEmotion=真;}; 情绪=任何类型{情绪=无聊;颜色=#8A2BE2;EmotionId=3; IsHappyEmotion=false; }; 情绪=任何类型{情绪=悲观;颜色=#48D1CC;EmotionId=22; IsHappyEmotion=真;}; 情绪=任何类型{情绪=沮丧;颜色=#FFC0CB;EmotionId=24;IsHappyEmotion=false; }; 情绪=任何类型{情绪=失望;颜色=#D2691E;EmotionId=7; IsHappyEmotion=false; }; 情绪=任何类型{情绪=怀疑;颜色=#CAFF70;EmotionId=9; IsHappyEmotion=false; }; 情绪=任何类型{情绪=担心;颜色=#008000;EmotionId=23; IsHappyEmotion=false; }; 情绪=任何类型{情绪=责备;颜色=#7CFC00;EmotionId=2; IsHappyEmotion=false; }; 情绪=任何类型{情绪=沮丧;颜色=#ADFF2F;EmotionId=8; IsHappyEmotion=false; }; 情绪=任何类型{情绪=嫉妒;颜色=#006400;EmotionId=16; IsHappyEmotion=false; }; 情绪=任何类型{情绪=内疚;颜色=#FFFF00;EmotionId=13; IsHappyEmotion=false; }; 情绪=任何类型{情绪=孤独;颜色=#BDB76B;EmotionId=18; IsHappyEmotion=false; }; 情绪=任何类型{情绪=恐惧;颜色=#FF8C00;EmotionId=11; IsHappyEmotion=false; }; 情绪=任何类型{情绪=愤怒;颜色=#FF0000;EmotionId=1; IsHappyEmotion=false; }; 情绪=任何类型{情绪=抑郁;颜色=#8B0000;EmotionId=5; IsHappyEmotion=false; }; 情绪=任何类型{情绪=绝望;颜色=#CD5C5C;EmotionId=6; IsHappyEmotion=false; }; 情绪=任何类型{情绪=仇恨;颜色=#800000;EmotionId=14; IsHappyEmotion=false; }; } 情绪=任何类型{情绪=仇恨;颜色=#800000;EmotionId=14; IsHappyEmotion=false; }; } 情绪=任何类型{情绪=仇恨;颜色=#800000;EmotionId=14; IsHappyEmotion=false; }; }
Thanks in Advance
提前致谢
采纳答案by Jitender Dev
Try this method and check if it works
试试这个方法并检查它是否有效
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();
}
}
This is Soap Parser Class
这是肥皂解析器类
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));
}
}
}
}
}
}
Also Tutorial with Source code
还有源代码教程
see this link
看到这个链接
回答by Kalai.G
Try this code snippet
试试这个代码片段
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;
}