簡體   English   中英

通過Soap Web服務獲得的響應為零

[英]Getting response is zero from soap web service

我想通過發送兩個參數在Android執行加法操作。我使用KSOAP來執行此操作。 這是WSDL文件 代碼如下。

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);
    }
}
}

當執行加,減等任何操作時,響應為零。
請任何人給出建議以獲得實際結果。

尚不清楚您的代碼有什么問題,但是您可以嘗試使用我的代碼,它可以正常工作。

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();
        }
    }

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM