Servicio web de Android y .NET: análisis del xml @ devuel
Así que hice algunas cosas "menores" con los servicios web y tuve cierto éxito, pero cuando intenté jugar con algunas cosas por diversión, me encontré con lo que creo que estoy analizando incorrectamente los datos para obtener la información que necesitar. Puedo obtener todo el xml como una cadena, pero no solo los 3 programas que necesito. Los getPrograms y getInstitutions son lo suficientemente similares que si puedo analizar los programas correctamente, puedo obtener las instituciones. Donde me estoy confundiendo es lo que creo que son las "etiquetas" y cuánto difieren de los tutoriales a lo que realmente estoy usando. También seguí el camino de XmlPullParser pero no sé si esta es la mejor manera de hacerlo (¿verdad?). El formato de lo que estoy accediendo es el siguiente:
<DataSet>
<xs:schema id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="tblPrograms">
<xs:complexType>
<xs:sequence>
<xs:element name="Program" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram>
<NewDataSet>
<tblPrograms diffgr:id="tblPrograms1" msdata:rowOrder="0">
<Program>Ancillary</Program>
</tblPrograms>
<tblPrograms diffgr:id="tblPrograms2" msdata:rowOrder="1">
<Program>Ancillary ESY</Program>
</tblPrograms>
<tblPrograms diffgr:id="tblPrograms3" msdata:rowOrder="2">
<Program>REAP</Program>
</tblPrograms>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
Y mi código fuente es el siguiente:
public class FirstScreen extends Activity {
/** Called when the activity is first created. */
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.ces.org/android/android.asmx";//must point to where service is located
/** HelloAndroid method */
//SOAP_ACTION = NAMESPACE + METHODNAME
private static final String SOAP_ACTION = "http://tempuri.org/HelloAndroid";
private static final String METHOD_NAME = "HelloAndroid";
/** SelectInstitutionTypes method */
//SOAP_ACTION = NAMESPACE + METHODNAME
private static final String SOAP_ACTION_INSTITUTIONS = "http://tempuri.org/SelectInstitutionTypes";
private static final String METHOD_NAME_INSTITUTIONS = "SelectInstitutionTypes";
/** SelectPrograms method */
//SOAP_ACTION = NAMESPACE + METHODNAME
private static final String SOAP_ACTION_PROGRAMS = "http://tempuri.org/SelectPrograms";
private static final String METHOD_NAME_PROGRAMS = "SelectPrograms";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getHelloAndroid();
//getInstitutionTypes();
getPrograms();
}//end of onCreate
private void getPrograms() {
TextView tv3 = (TextView)findViewById(R.id.TextView03);//contains SelectInstitutionTypes information
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_PROGRAMS);
//soap serialization
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//running 1.1
soapEnvelope.dotNet=true;//to handle .net services asmx/aspx
soapEnvelope.setOutputSoapObject(request);//package request
//create transport object(s)
HttpTransportSE aht = new HttpTransportSE(URL);
try
{
aht.debug = true;
//make the call
aht.call(SOAP_ACTION_PROGRAMS, soapEnvelope); //in/out
SoapObject resultString = (SoapObject)soapEnvelope.getResponse();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader (resultString.toString()));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if(eventType == XmlPullParser.START_TAG) {
if(xpp.getName().equals("Program"))
System.out.println("Start tag "+xpp.getName());
System.out.println("Program"+ xpp.getAttributeName(0));
} else if(eventType == XmlPullParser.END_TAG) {
System.out.println("End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
System.out.println("Text "+xpp.getText());
}
eventType = xpp.next();
}
System.out.println("End document");
tv3.setBackgroundColor(Color.BLUE);
tv3.setText("STATUS: " + resultString /*ks.toString()*/ + "\n\n" + "AHTHOST: " +
aht.getHost() + "\n\n" + "NAHT STRING: " + aht.toString());
}
catch(Exception e)
{
e.toString();
e.printStackTrace();
tv3.setText("EXCEPTION NAME: " + e.toString().toString() +
"\n\n" + "EXCEPTION MESSAGE: " + e.getMessage() + " ");
}
}//end of getPrograms
private void getInstitutionTypes() {
TextView tv2 = (TextView)findViewById(R.id.TextView02);//contains SelectInstitutionTypes information
//tv2.setText("TODO: SelectInstitutionTypes");
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_INSTITUTIONS);
//soap serialization
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//running 1.1
soapEnvelope.dotNet=true;//to handle .net services asmx/aspx
soapEnvelope.setOutputSoapObject(request);//package request
//create transport object(s)
HttpTransportSE aht = new HttpTransportSE(URL);
try
{
aht.debug = true;
//make the call
aht.call(SOAP_ACTION_INSTITUTIONS, soapEnvelope); //in/out
SoapObject resultString = (SoapObject)soapEnvelope.getResponse();
tv2.setText("STATUS: " + resultString /*ks.toString()*/ + "\n\n" + "AHTHOST: " +
aht.getHost() + "\n\n" + "NAHT STRING: " + aht.toString());
}
catch(Exception e)
{
e.toString();
e.printStackTrace();
tv2.setText("EXCEPTION NAME: " + e.toString().toString() +
"\n\n" + "EXCEPTION MESSAGE: " + e.getMessage() + " ");
}
}//end of getInstitutionTypes
public void getHelloAndroid() {
TextView tv = (TextView)findViewById(R.id.TextView01);//contains HelloAndroid information
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//soap serialization
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//running 1.1
soapEnvelope.dotNet=true;//to handle .net services asmx/aspx
soapEnvelope.setOutputSoapObject(request);//package request
//create transport object(s)
HttpTransportSE aht = new HttpTransportSE(URL);
try
{
aht.debug = true;
//make the call
aht.call(SOAP_ACTION, soapEnvelope); //in/out
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
tv.setBackgroundColor(Color.BLUE);
tv.setText("STATUS: " + resultString + "\n\n" + "AHTHOST: " +
aht.getHost() + "\n\n" + "NAHT STRING: " + aht.toString());
}
catch(Exception e)
{
e.toString();
e.printStackTrace();
tv.setText("EXCEPTION NAME: " + e.toString().toString() +
"\n\n" + "EXCEPTION MESSAGE: " + e.getMessage() + " ");
}
}//end of getHelloAndroid
}//end of activity