简体   繁体   中英

How to use xml contents in web service by SOAP method on android/eclipse

Has any one me this issue,now i am consuming a web service by SOAP method on android with eclipse ide.Well its working fine.Please find my service link

" http://54.251.60.177/MobileWS/Student.asmx?wsdl "

"GetStudentInformation", this is the service i am working out with and its input value is "111105009"

If i give this value as input on the above service,its just showing the result like the below images on the emulator and also showing these things publicly.

Image_1

在此处输入图片说明

Image_2 在此处输入图片说明

But here my problem is,the above image_2 is showing all the xml tags with answers,but i need to show only the answers like what i mentioned on the image_2.How to achieve this concept?

Please find my sources below

WebMethod.java

public class GetStudentInformation 
{
public String GetStudentInformation(String sStudentCode)
{
    return sStudentCode;
}    }

Demo_tabActivity.java

public class Demo_tabActivity extends Activity 
{
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME = "GetStudentInformation";
private static String SOAP_ACTION = "http://tempuri.org/GetStudentInformation";
private static String URL = "http://54.251.60.177/MobileWS/Student.asmx?WSDL";

Button btnFar;
EditText txtFar;

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

       txtFar = (EditText)findViewById(R.id.editText_in);
       btnFar = (Button)findViewById(R.id.button1);
       btnFar.setOnClickListener(new View.OnClickListener()
       {
       public void onClick(View v)
       {
         String b;

         //Initialize soap request + add parameters
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

         //Use this to add parameters

         request.addProperty("sStudentCode",txtFar.getText().toString());

         //Declare the version of the SOAP request

         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

         envelope.setOutputSoapObject(request);

         envelope.dotNet = true;

         try 
         {
             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

             //this is the actual part that will call the webservice

             androidHttpTransport.call(SOAP_ACTION, envelope);

             // Get the SoapResult from the envelope body.

             SoapPrimitive result = (SoapPrimitive)envelope.getResponse();


             if (result != null)
             {
              //Get the first property and change the label text

                b = result.toString();
                Intent myIntent = new Intent(Demo_tabActivity.this, Tabhost.class);

                myIntent.putExtra("result1", b);
                startActivity(myIntent);
             }
             else
             {
                Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_SHORT).show();
             }
         }
        catch (Exception e)
        {
           e.printStackTrace();
           }
         }
       });
   }
}

Tabhost.java

public class Tabhost extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    String result1;

    Bundle extras = getIntent().getExtras();

    if(extras!=null)
    {
        result1 = extras.getString("result1");
    } else result1 = "Didnt work !" ;



    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, Tab_1.class);
    intent.putExtra("result1", result1);
    spec = tabHost.newTabSpec("first").setIndicator("First").setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Tab_2.class);
    spec = tabHost.newTabSpec("second").setIndicator("Second").setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
 }
 }

Tab_1.java

public class Tab_1 extends Activity 
{
TextView tv1;
String result1;

/** Called when the activity is first created. */

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

Bundle extras = getIntent().getExtras();
if(extras != null)
{   
    result1 = extras.getString("result1");
} 
else result1 = "didnt work";

    tv1 = (TextView)findViewById(R.id.textView_main2);
    tv1.setText(result1);


}
}

Tab_2.java

public class Tab_2 extends Activity {


/** Called when the activity is first created. */   

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main3);
    }
}

Thanks for your precious time!..

  • The XML Result String that you are getting is to be Parsed.Ypu can parse that like this using Dom Parsing.
  • Here you have to create a model class that will save the Details after parsing the Data.

StudentModel.java

package com.cws.model;

public class StudentModel
{
private static String StudentName;
private static String NoOfDayPresent;
private static String NoOfDayAbsent;

public String getStudentName(){
    return StudentName;
}
public void setStudentName(String studentname){
    StudentName=studentname;
}

    public String getNoOfDayPresent(){
    return NoOfDayPresent;
}
public void setNoOfDayPresent(String noOfDayPresent){
    NoOfDayPresent=noOfDayPresent;
}   
    ....Similarly Create getter and setter methhods for remaining variables Here
}  

Now call the parsing Method from the Activity having the Value of Result in this way

StudentModel student=new StudentModel();
Parsing parsing=new Parsing();  
student=parsing.parseByDOM(result1);

Now you can access the values in this way..

mytextView1.setText(student.getStudentName());
.
mytextView2.setText(student.getNoOfDayPresent());
.
.

Parsing.class

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.cws.model.ResumeCover;

public class StudentDataParser {

public StudentModel parseByDOM(String response) throws ParserConfigurationException, 
                                                                                    SAXException, IOException 
{
            StudentModel sm=new StudentModel();
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new InputSource(new StringReader(response)));
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("row");
                for (int temp = 0; temp < nList.getLength(); temp++) 
                {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) 
                    {
                        Element eElement = (Element) nNode;
                        sm.setStudentName(getTagValue("FirstName", eElement));
                        sm.setNoOfDayPresent(getTagValue("NoOfDayPresent", eElement));
                <----Similarly set All the Values Of all variables-->           
                    }
                }

        return sm;
    }



private static String getTagValue(String sTag, Element eElement) 
    {

        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        if(nlList.getLength()>0)
        {
            Node nValue = (Node) nlList.item(0);
            return nValue.getNodeValue();   
        }
        else
            return "-";

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM