通过Soap Web服务获得的响应为零

时间:2018-06-21 06:15:37

标签: java android soap-client ksoap2

我想通过发送两个参数在Android中执行加法运算。我使用KSOAP来执行此操作。 这是WSDL file。 代码如下。

public class MainActivity extends AppCompatActivity {

EditText editText1,editText2;
Button button;
TextView textView;

String URL = "http://www.dneonline.com/calculator.asmx?WSDL";
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "Add";
String SOAP_ACTION = "http://tempuri.org/Add";

String result = "";



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

    button = (Button) findViewById(R.id.btn);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            new client().execute();
        }

    });

}

class client extends AsyncTask<String, Void, String> {


    @Override
    protected String doInBackground(String... params) {

        editText1 = (EditText) findViewById(R.id.et1);
        editText2 = (EditText) findViewById(R.id.et2);

        final int a = Integer.parseInt(editText1.getText().toString());
        final int b = Integer.parseInt(editText2.getText().toString());

        SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

        PropertyInfo propertyInfo = new PropertyInfo();
        propertyInfo.setName("intA");
        propertyInfo.setValue(a);
        propertyInfo.setType(Integer.class);
        soapObject.addProperty(propertyInfo);

        propertyInfo = new PropertyInfo();
        propertyInfo.setName("intB");
        propertyInfo.setValue(b);
        propertyInfo.setType(Integer.class);
        soapObject.addProperty(propertyInfo);


       // propertyInfo.setNamespace(NAMESPACE);
       // propertyInfo.setMultiRef(true);


        SoapSerializationEnvelope envelope =  new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(soapObject);
        envelope.implicitTypes = true;
        HttpTransportSE httpTransportSE = new HttpTransportSE(URL,600000);

        try {


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

        return result;
    }

    @Override
    protected void onPostExecute(String s) {

        textView = (TextView)findViewById(R.id.text);
        textView.setText("Sum is = " + result);
    }
}
}

在执行加法,减法等任何操作时,响应都为零。
请任何人提出建议以获得实际结果。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您的代码有什么问题,但是您可以尝试使用它,它可以正常工作。

public class MainActivity extends AppCompatActivity {

    class Calculator {
        public String URL = "http://www.dneonline.com/calculator.asmx?WSDL";
        public String NAMESPACE = "http://tempuri.org/";
        public String METHOD_NAME = "Add";
        public String SOAP_ACTION = "http://tempuri.org/Add";

        public String Add(String first, String second) {
            String fahrenheit;

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("intA", first);
            request.addProperty("intB", second);

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;

            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                fahrenheit = response.toString();
            } catch (Exception e) {
                e.printStackTrace();
                fahrenheit = null;
            }
            return fahrenheit;
        }
    }

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

    }

    class Client extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String answer = new Calculator().Add("1", "1");
            return answer == null ? "error" : answer;
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(MainActivity.this, s, LENGTH_LONG).show();
        }
    }

} 
相关问题