我们正在尝试使用http://www.webserviceX.NET/CurrencyConvertor.asmx上的 Web 服务制作货币转换器 我们已经到处搜索我们的问题的答案,但即使是其他用户的相同问题的解决方案也不起作用对我们来说,我们不知道该怎么做。问题是我们的结果一直为空......这是我们的代码:
package com.example.ws;
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;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tvResult;
Button btnConvert;
final String NAME_SPACE = "http://www.webserviceX.NET/";
String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";
final String METHOD_NAME = "ConversionRate";
final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tvPretvoreniIznos);
btnConvert = (Button) findViewById(R.id.btnPretvori);
btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);
PropertyInfo propInfo = new PropertyInfo();
propInfo.name = "FromCurrency";
propInfo.type = PropertyInfo.STRING_CLASS;
propInfo.setValue("USD");
PropertyInfo propInfo2 = new PropertyInfo();
propInfo2.name = "ToCurrency";
propInfo2.type = PropertyInfo.STRING_CLASS;
propInfo2.setValue("EUR");
request.addProperty(propInfo);
request.addProperty(propInfo2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
tvResult.setText(resultsRequestSOAP.toString());
}
catch (Exception e) {
tvResult.setText(e.getMessage());
}
}
});
}
}
我们发现其他人以前也遇到过这个问题,并且简单的解决方案对他们有用,我们尝试了所有方法,但没有成功。
我在 AsyncTask 中进行了网络活动,但没有成功。这是代码:
package com.example.ws;
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;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tvResult;
Button btnConvert;
final String NAME_SPACE = "http://www.webserviceX.NET/";
String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";
final String METHOD_NAME = "ConversionRate";
final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
String res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tvPretvoreniIznos);
btnConvert = (Button) findViewById(R.id.btnPretvori);
btnConvert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new convert().execute();
}
});
}
public class convert extends AsyncTask<String, Void, String> {
SoapObject result = null;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME);
PropertyInfo propInfo = new PropertyInfo();
propInfo.name = "FromCurrency";
propInfo.type = PropertyInfo.STRING_CLASS;
propInfo.setValue("USD");
PropertyInfo propInfo2 = new PropertyInfo();
propInfo2.name = "ToCurrency";
propInfo2.type = PropertyInfo.STRING_CLASS;
propInfo2.setValue("EUR");
request.addProperty(propInfo);
request.addProperty(propInfo2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
res = resultsRequestSOAP.toString();
} catch (Exception e) {
tvResult.setText(e.getMessage());
}
return null;
}
protected void onPostExecute(String r) {
String res = r;
tvResult.setText(res);
}
}
}
你能检查一下我做错了什么吗?