我们正试图在http://www.webserviceX.NET/CurrencyConvertor.asmx使用网络服务制作货币转换器 我们已经到处寻找我们问题的答案,但即使是与其他用户相同问题的解决方案也不适用于我们,我们不知道该怎么做。问题是我们的结果保持为空... 这是我们的代码:
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);
}
}
}
请你检查一下我做错了什么?
答案 0 :(得分:1)
这篇文章有2年了,但我找到了问题的解决方案。你有两个错误,一个是你不能覆盖onPostExecute方法;另一种是,你必须使用全局“String res”变量。所以代码必须是:
@Override
protected void onPostExecute(String r) {
tvResult.setText(res);
}
答案 1 :(得分:0)
您正在尝试访问Main on Main(UI线程)。这不再允许,从上面开始Honeycomb。您可以选择AsyncTask
中的网络活动。请参阅此SO question中接受的答案。