1

我想在 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 数据?

4

6 回答 6

0

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

您还需要,

  • commons-httpclient-3.0.1.jar
  • ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

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

以下是相同的伪代码,

responseDump = new String(buf);
unescape(responseDump);
your_string = responseDump;

然后your_string会有完整的 xml 响应。

于 2012-05-23T05:13:49.747 回答
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);
于 2012-05-23T05:17:32.353 回答
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);
        }
       });
       }

}

于 2012-05-23T05:33:24.647 回答
0

嗨,这可能对你有用http://www.codeproject.com/Articles/112381/Step-by-Step-Method-to-Access-Webservice-from-Andr

于 2012-05-23T05:09:46.903 回答
0

使用 DOM Parser 或 SAX Parser 会更好......这个链接肯定对你有用:

于 2012-05-23T07:04:21.890 回答
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;
}
于 2017-08-27T07:48:28.597 回答