简体   繁体   中英

How to use SOAP web service in android

I am using trying to integrate SOAP web service in android, i no enough idea about how soap web service work, after searching to internet i have integrated kSOAP lib in android studio and write this code

public class SOAP extends AppCompatActivity {

String URL = "http://www.getrixi.com/GoTukxiWebServices/TukxiService.asmx";
String NAMESPACE = "http://tempuri.org/GetAssets";
String SOAP_ACTION = "http://tempuri.org/GetAssets";
String METHOD_NAME = "GetAssets";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_soap);

    Button bt_id = (Button) findViewById(R.id.bt_id);
    bt_id.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new CallWebService().execute();

        }
    });
}

class CallWebService extends AsyncTask<String, Void, String> {
    @Override
    protected void onPostExecute(String s) {
       Log.e("TAG", "the result from server is: " + s);
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";

        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

        soapObject.addProperty("Lat", "31.71");
        soapObject.addProperty("Long", "74.34");
        soapObject.addProperty("Radius", "30");
        soapObject.addProperty("AssetType", "all");


       /* PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setName("Lat");
        propertyInfo.setName("Long");
        propertyInfo.setName("Radius");
        propertyInfo.setName("AssetType");

        propertyInfo.setValue(params[0]);
        propertyInfo.setValue(params[1]);
        propertyInfo.setValue(params[2]);
        propertyInfo.setValue(params[3]);
        propertyInfo.setType(String.class);

        soapObject.addProperty(propertyInfo);*/

        SoapSerializationEnvelope envelope =  new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(soapObject);

        HttpTransportSE httpTransportSE = new HttpTransportSE(URL);

        try {
            httpTransportSE.call(SOAP_ACTION, envelope);
            SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
            Log.e("TAG", "the message result : " + soapPrimitive);
            result = soapPrimitive.toString();
            Log.e("TAG", "the resul is: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}

how can i get json result i have test the api here http://getrixi.com/GoTukxiWebServices/TukxiService.asmx?op=GetAssets it is working fine on web with get method How can i get result in android

This piece of code can be more illustrative :

 String str="http://your.domain.here/yourSubMethod";
    URLConnection urlConn = null;
    BufferedReader bufferedReader = null;
    try
    {
        URL url = new URL(str);
        urlConn = url.openConnection();
        bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = bufferedReader.readLine()) != null)
        {
            stringBuffer.append(line);
        }

        return new JSONObject(stringBuffer.toString());
    }
    catch(Exception ex)
    {
        Log.e("App", "yourDataTask", ex);
        return null;
    }

For details read Android: Fetching JSONARRAY Data from Web api

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