java - 从肥皂网络服务获得响应为零
问题描述
我想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();
}
}
}
推荐阅读
- php - Safari 没有获取 index.php
- javascript - 删除Javascript中括号之间的内容
- hibernate - TransactionManager / UserTransaction 不会反映我的数据库中的更改 - @Transactional 会。为什么?
- arrays - 使用 useState 成功将元素添加到数组后,map 方法不会渲染新元素
- orocommerce - 我正在尝试配置 Web 目录的配置,以便产品
- bash - 如何将我的 bash 脚本结果加载到特定位置
- python - 如何从需要“root”的类中调用函数
- git - 从 docker 容器内尝试登录失败后 Git 存储库无法访问
- azure - 如何在 Azure 应用配置中添加地图
- amazon-web-services - AWS CLI - 如何加密凭证