Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

java - How to parse the soap web service coming from server object?

I am able to get response from server while doing soap programming.My problem is the I am getting object from the server,Now I need to get values from server response .Here is my code and response

package com.example.networkconnectivity;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    AutoCompleteTextView autocompletetextview;

    String[] array = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten" };
    private static final String SOAP_ACTION = "http://wsendpoints.bbrailapps.firstgroup.com/getDestinationStationDashboard";
    private static final String METHOD_NAME = "getDestinationStationDashboard";
    private static final String NAMESPACE = "http://wsendpoints.bbrailapps.firstgroup.com";
    private static final String URL = "http://railapps.firstgroup.com/FirstGroupRailApps/services/RailAppsCAWS?wsdl";
    private SoapObject resultRequestSOAP = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        autocompletetextview = (AutoCompleteTextView)     findViewById(R.id.autocompletetextview);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.select_dialog_item, array);

        autocompletetextview.setThreshold(2);

        autocompletetextview.setAdapter(adapter);


        System.out.println("=====================" + haveNetworkConnection());

        new AsyncTask<Void, Void, Object>() {
            ProgressDialog dialog = new ProgressDialog(MainActivity.this);
            private PropertyInfo pi1;
            private String SoapResult;

            protected void onPreExecute() {
                dialog.show();
            };

            protected Object doInBackground(Void[] params) {

                /*resultRequestSOAP = new SoapObject(NAMESPACE, METHOD_NAME);

                SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envp.dotNet = true;
                envp.setOutputSoapObject(resultRequestSOAP);
                AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                        URL);
                try {
                    androidHttpTransport.call(SOAP_ACTION, envp);
                    SoapPrimitive resultsString = (SoapPrimitive)envp.getResponse();
                    System.out.println("resultsString"+resultsString);
                    return resultsString.toString();

                } catch (Exception e) {
 System.out.println("---------------------------"+e.toString());
 Log.d("ppp", e.toString());
                    Toast.makeText(Soapclass.this,
                            "Check Network connectivety" + e.toString(),
                            Toast.LENGTH_LONG).show();
                    Log.v("WS Error->", e.toString());
                    return e.toString();
                }*/

                resultRequestSOAP = new SoapObject(NAMESPACE, METHOD_NAME);
                 pi1 = new PropertyInfo();
                 pi1.setName("crsCode");
                 pi1.setValue("HNH");//get the string that is to be sent to the web service
                 pi1.setType(String.class);
                 resultRequestSOAP.addProperty(pi1);
                SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envp.dotNet = true;
                envp.setOutputSoapObject(resultRequestSOAP);
                AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                        URL);
                try {
                    androidHttpTransport.call(SOAP_ACTION, envp);
                    if (envp.bodyIn instanceof SoapFault) {
                        SoapResult = ((SoapFault) envp.bodyIn).faultstring;
                        System.out.println("==SoapResulght==="+SoapResult);
                    } else {
                        SoapObject resultsRequestSOAP = (SoapObject) envp.bodyIn;
                        SoapResult = resultsRequestSOAP.getProperty(0).toString();
                        System.out.println("==SoapResult==="+SoapResult);
                    }
//                    SoapObject obj = (SoapObject)envp.getResponse(); 
//                  
//                    System.out.println("resultsString"+obj);
                    return SoapResult;

                } catch (Exception e) {
 System.out.println("---------------------------"+e.toString());
 Log.d("ppp", e.toString());
                    Toast.makeText(MainActivity.this,
                            "Check Network connectivety" + e.toString(),
                            Toast.LENGTH_LONG).show();
                    Log.v("WS Error->", e.toString());
                    return e.toString();
                }


            };

            protected void onPostExecute(Object result) {
                dialog.dismiss();
                Toast.makeText(MainActivity.this,
                        "Check Network connectivety" + result,
                       Toast.LENGTH_LONG).show();
            };
        }.execute();

    }

    public boolean haveNetworkConnection() {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

}

System.out.println("==SoapResult==="+SoapResult);

Response

anyType{RID=201309201377831; alertDetailPopulated=false; alertsId=0; alertsSummary=null; destExpArrival=08:01; destSchArrival=08:00; destinationStation=anyType{crsCode=BKJ; stationName=Beckenham Junction; }; expArrival=07:49; expDepart=07:49; otherAlertPresent=false; platformNo=3; routeDetailPopulated=false; routeDetails=null; rsID=null; schArrival=07:49; schDepart=07:49; serviceAlertPresent=false; toc=SE; tocName=Southeastern; trainID=2M18; trainLastReportedAt=null; }

Now I want to print RID and alertDetailPopulated values.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
       @Override
protected SoapObject doInBackground(String... params) {
    // TODO Auto-generated method stub
    Intent i=getIntent();
     String a=i.getStringExtra("code");
     CallSoap cs=new CallSoap();
     resp= cs.Call(a);
     System.out.println("ssssssssssssssssssssssssssssssssss"+resp);
     return resp;
}
protected void onPostExecute(Object result) {
    if(result!= null)
    {
  String[] testValues = new String[result.getPropertyCount()];
    for(int j= 0; j< result.getPropertyCount(); j++){
        testValues[j] = result.getProperty(j).toString(); 
        String name=(result.getProperty(0).toString());

            Toast.makeText(MainActivity.this,
                    "Check Network connectivety" + name
                   Toast.LENGTH_LONG).show();
        };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...