从 soap Web 服务获得响应为零



我想通过发送两个参数在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();
}
}
} 

最新更新