如何在Android中解析SOAP WebServices XML数据?

时间:2012-05-23 05:04:23

标签: android web-services soap xml-parsing

我希望在Android肥皂网络服务中将输入请求作为XML发送,最后我希望在Xml中显示响应。我可以在Android中使用那些肥皂网络服务。

 package com.venkattt.pack;

import java.net.SocketException;

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

import android.app.Activity;
import android.os.Bundle;

public class SoapWebservicesExampleActivity extends Activity {
    /** Called when the activity is first created. */
      final String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style";
     final String URL = "http://**********:8000/sap/bc/srt/wsdl/srvc_14DAE9C8D79F1EE196F1FC6C6518A345/wsdl11/allinone/ws_policy/document?sap-client=800&sap-user=*******&sap-password=*******";
      final String METHOD_NAME = "Z_GET_CUST_GEN";
     final String SOAP_ACTION = "urn:sap-com:document:sap:soap:functions:mc-style/Z_GET_CUST_GEN";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // set up
            request.addProperty("Input", "1460");
            request.addProperty("Langu", "d");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); // put all required data into a soap
            envelope.dotNet = false;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(URL);
            httpTransport.debug = true;

            try {

                 httpTransport.call(SOAP_ACTION, envelope);
                 Object result = (Object) envelope.getResponse();

                 System.out.println("theeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee result"+result);

                }
            catch(SocketException ex){
                    ex.printStackTrace();
                } catch (Exception e) {
                   e.printStackTrace();
                }
        }
    }

我编写了使用Android服务器的代码,但我没有得到xml数据?

6 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

为此你必须下载两个文件,

您还需要,

  • 公地HttpClient的-3.0.1.jar
  • ksoap2-机器人组装-2.5.4-罐与 - dependencies.jar

然后在类call()的{​​{1}}方法中,您可以将xml响应分配给您定义的String,并可以进一步访问它。

下面是相同的伪代码,

AndroidHttpTransport

然后responseDump = new String(buf); unescape(responseDump); your_string = responseDump; 将获得完整的xml响应。

答案 2 :(得分:0)

       HttpPost httppost = new HttpPost("http://192.168.1.103/test/index1.php");
        //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("log_tag", "connection success ");
     //convert response to string

        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {
            sb.append(line + "\n");

        }
        is.close();
        result=sb.toString();
        //Log.e("",""+result);

答案 3 :(得分:0)

public class From extends Activity 
{
HttpPost httppost;
InputStream is;
ListView lv;
TextView displayStrip;
ArrayList<String> atcodeid=new ArrayList<String>();
ArrayAdapter<String> adapter;
ArrayList<String> listItems=new ArrayList<String>();
ArrayList<String> originalname=new ArrayList<String>();
String mode;
Button btn_name;
Button btn_map;
SharedPreferences prefs;
int testing;
String shortcutname;
@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.from);

    lv          =   (ListView)findViewById(R.id.listView1);
    displayStrip=   (TextView)findViewById(R.id.displayStrip);
    btn_name    =   (Button)findViewById(R.id.name);
    btn_map     =   (Button)findViewById(R.id.map);
    prefs       =    PreferenceManager.getDefaultSharedPreferences(this);
    testing     =    prefs.getInt("settings",0);

    adapter=new ArrayAdapter<String>(From.this,
            android.R.layout.simple_list_item_1,
            listItems);
    lv.setAdapter(adapter);
    lv.setFastScrollEnabled(true);
    String result = "";
   // ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
   // nameValuePairs.add(new BasicNameValuePair("birthyear","1990"));
    try{
        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("http://192.168.1.103/test/index1.php");
        //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        //Log.e("log_tag", "connection success ");
      //  Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }
    //convert response to string
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {
            sb.append(line + "\n");
           // Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }
        is.close();
        result=sb.toString();
        //Log.e("",""+result);
    }catch(Exception e){
           Log.e("log_tag", "Error converting result "+e.toString());
    }
    try{
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data=null;
           for(int i=0;i<jArray.length();i++)
           {
                json_data =jArray.getJSONObject(i);
                String src_name=json_data.getString("commonname");
                originalname.add(src_name);
                if(src_name.contains("Wharf"))
                {
                    shortcutname=src_name.substring(0, src_name.indexOf("Wharf"));
                }
                else if(src_name.contains("Station"))
                {
                    shortcutname=src_name.substring(0, src_name.indexOf("Station"));
                }
                else 
                    shortcutname=src_name;
                listItems.add(shortcutname);
                //listItems.add(src_name);
                //Log.e("",i+"testing"+src_name);
                atcodeid.add(json_data.getString("Atcocode"));
                adapter.notifyDataSetChanged();
           }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
            Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
    }
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView arg0,
        View arg1, int position, long arg3) {
        Intent i = new Intent(From.this, To.class);
        //Toast.makeText(From.this, ""+atcodeid.get(position)+" "+originalname.get(position), Toast.LENGTH_SHORT).show();
        i.putExtra("mode", mode);
        i.putExtra("id", atcodeid.get(position));
        i.putExtra("fromacto", originalname.get(position));
        startActivity(i);
        }
        });
    btn_map.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            btn_map.requestFocus();
            btn_map.setFocusable(true);
            SharedPreferences.Editor prefsEditor = prefs.edit();
            prefsEditor.putInt("settings", 1);
            prefsEditor.commit();
            Intent intent=new Intent(From.this , FromGoogleMaps.class);
            startActivity(intent);
        }
       });
    btn_name.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            btn_name.requestFocus();
            btn_name.setFocusable(true);
            SharedPreferences.Editor prefsEditor = prefs.edit();
            prefsEditor.putInt("settings", 0);
            prefsEditor.commit();
            Intent intent=new Intent(From.this , From.class);
            startActivity(intent);
        }
       });
       }

}

答案 4 :(得分:0)

答案 5 :(得分:0)

当您致电HttpTransport.responseDump

时,您将能够获得结果

这是我实施的方式

public String GetNewTransaction_service(String login_id,String Password_id)
{



    String SOAP_ACTION = "http://www.webserviceX.NET/GetWeather";
    String METHOD_NAME = "GetWeather";
    String NAMESPACE  = "http://www.webserviceX.NET";
    String SOAP_ADDRESS = "http://www.webservicex.com/globalweather.asmx";

    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
    request.addProperty("login",login_id);
    request.addProperty("pwd",Password_id);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    envelope.implicitTypes = true;

    HttpTransportSE httpTransaport = new HttpTransportSE(SOAP_ADDRESS);
    httpTransaport.debug = true;

    String response = null;


    try
    {
        httpTransaport.call(SOAP_ACTION,envelope);
        response = httpTransaport.responseDump;
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  response;
}