简体   繁体   English

从Android消费SOAP Web服务

[英]Consuming SOAP web services from Android

I am trying to create a project in android studio to consume a web service from android. 我试图在android studio中创建一个项目,以使用android的网络服务。 My app compiles without error but when I run it the required output is not displayed as if it is not working at all. 我的应用程序编译没有错误,但是当我运行它时,所需的输出没有显示出来,好像它根本无法正常工作。

Web Service Links:- 网络服务链接:

http://202.54.216.49/logs/test.asmx http://202.54.216.49/logs/test.asmx

http://202.54.216.49/logs/test.asmx?WSDL http://202.54.216.49/logs/test.asmx?WSDL

MainActivity.java- MainActivity.java-

package com.example.abhimanyu.ws_soap;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    EditText textBox,textBox2;
    Button button;
    TextView ans;

    String URL = "http://202.54.216.49/logs/test.asmx?WSDL";
    String NAMESPACE = "http://tempuri.org";
    String SOAP_ACTION = "http://tempuri.org/calculation";
    String METHOD_NAME = "calculation";
    String PARAMETER_NAME1 = "a";
    String PARAMETER_NAME2 = "b";

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

        textBox = (EditText)findViewById(R.id.txtBox);
        textBox2 = (EditText)findViewById(R.id.txtBox2);
        button = (Button)findViewById(R.id.btn);
        ans = (TextView)findViewById(R.id.answer);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CallWebService().execute(textBox.getText().toString(),textBox2.getText().toString());
            }
        });
    }

    class CallWebService extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String s) {
            ans.setText(s);
        }

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

            SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo propertyInfo1 = new PropertyInfo();
            propertyInfo1.setName(PARAMETER_NAME1);
            propertyInfo1.setValue(params[0]);
            propertyInfo1.setType(String.class);

            PropertyInfo propertyInfo2 = new PropertyInfo();
            propertyInfo2.setName(PARAMETER_NAME2);
            propertyInfo2.setValue(params[1]);
            propertyInfo2.setType(String.class);

            soapObject.addProperty("a",propertyInfo1);
            soapObject.addProperty("b",propertyInfo2);

            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();
                result = soapPrimitive.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return result;

        }
    }
    }

activity_main.xml- activity_main.xml-

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context="com.example.abhimanyu.ws_soap.MainActivity">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtBox"
    android:layout_marginBottom="5dp" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtBox2"
    android:layout_below="@id/txtBox"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Calculate"
    android:id="@+id/btn"
    android:layout_below="@+id/txtBox2"
    android:layout_marginBottom="5dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/answer"
    android:layout_below="@id/btn"/>
</RelativeLayout>

Note: I copied this code from internet and made some adjustments according to my requirements. 注意:我从互联网复制了此代码,并根据需要进行了一些调整。 But, I'm new in android studio so most probably those adjustments did something wrong. 但是,我是android studio的新手,所以很可能这些调整做错了什么。

Try this. 尝试这个。

public class test extends AsyncTask<String,Void,Integer>{
    int a;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(String... params) {
            String value1 = params[0];
            String value2 = params[1];
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("a", Integer.valueof(value1));
            request.addProperty("b", Integer.valueof(value2));
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, 60 * 10000);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive soapPrimitive = (SoapPrimitive) envelope.getResponse();
            a = new Integer(Integer.valueOf(soapPrimitive.toString()));
        }catch (Exception e){

        }

        return a;
    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        Toast.makeText(MeetActivity.this, ""+String.valueOf(integer), Toast.LENGTH_SHORT).show();
    }
}

This is my final code and now its working perfectly fine. 这是我的最终代码,现在可以正常工作。 Thanks for help though. 谢谢您的帮助。

package com.example.abhimanyu.ws_soap;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    EditText textBox, textBox2;
    Button button;
    TextView ans;


    String URL = "http://202.54.216.49/logs/test.asmx";
    String NAMESPACE = "http://tempuri.org/";
    String SOAP_ACTION = "http://tempuri.org/calculation";
    String METHOD_NAME = "calculation";
    String PARAMETER_NAME1 = "a";
    String PARAMETER_NAME2 = "b";

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

        textBox = (EditText) findViewById(R.id.txtBox);
        textBox2 = (EditText) findViewById(R.id.txtBox2);
        button = (Button) findViewById(R.id.btn);
        ans = (TextView) findViewById(R.id.answer);
        final Context context;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CallWebService().execute(textBox.getText().toString(), textBox2.getText().toString());
            }
        });
    }

    class CallWebService extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String s) {
            ans.setText(s);
        }

        @Override
        protected void onPreExecute() {
            Log.i("onPreexecute","running");
            super.onPreExecute();
        }

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

            SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo propertyInfo1 = new PropertyInfo();
            propertyInfo1.setName(PARAMETER_NAME1);
            propertyInfo1.setValue(params[0]);
            propertyInfo1.setType(String.class);
            soapObject.addProperty(propertyInfo1);

            PropertyInfo propertyInfo2 = new PropertyInfo();
            propertyInfo2.setName(PARAMETER_NAME2);
            propertyInfo2.setValue(params[1]);
            propertyInfo2.setType(String.class);
            soapObject.addProperty(propertyInfo2);

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

            HttpTransportSE httpTransportSE =  new HttpTransportSE(URL);

            try {
                httpTransportSE.call(SOAP_ACTION, envelope);
                SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
                result = soapPrimitive.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return result;

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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