我使用ksoap访问webservice,使用实时数据制作货币转换器。然而,来自webservice的响应是org.ksoap2.SoapFault.But这个答案不是作为例外抛出的。这个答案来到我的文本区域我的代码有什么问题?
这是源代码:
package com.example.deneme;
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.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView input;
TextView output;
Button convert;
private static final String NAMESPACE = "http://www.webserviceX.NET/";
private static String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";
private static final String METHOD_NAME = "ConversionRate";
private static final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
// Yararlanacağımız web servisin yerini belirtiyoruz.
/*
final static String NAMESPACE = "http://tempuri.org/";
// Web servis üzerinde hangi metodu kullanacağımızı ayarlıyoruz.
final static String METHOD_NAME = "CelsiusToFahrenheit";
// SOAP_ACTION : NAMESPACE ve METHOD_NAME birleşiminden oluşuyor.
final static String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
// Son olarak Web servise ait URL tanımlamasını burada yapıyoruz.
final static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText1);
convert=(Button)findViewById(R.id.convertt);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
/*
PropertyInfo propInfo=new PropertyInfo();
propInfo.name="FromCurrency";
propInfo.type=PropertyInfo.STRING_CLASS;
propInfo.setValue("GBP");
PropertyInfo propInfo2 = new PropertyInfo();
propInfo2.name="ToCurrency";
propInfo2.type = PropertyInfo.STRING_CLASS;
propInfo2.setValue("EUR");
*/
// request.addProperty(propInfo);
//request.addProperty(propInfo2);
request.addProperty("FromCurrency","GBP");
request.addProperty("ToCurrency","AFA");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet=true;
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
try {
httpTransport.call(SOAP_ACTION, envelope);
System.out.println("2!!!!!!!!!!!!!!!!");
SoapObject result = (SoapObject)envelope.bodyIn;
System.out.println("1!!!!!!!!!!!!!!!!");
input.setText(result.getProperty(0).toString());
} catch (Exception e) {
input.setText(e.getMessage());
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}][1]
[1]: http://i.stack.imgur.com/QtUoD.png
答案 0 :(得分:0)
在您的代码中有一个像这样的代码段,
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
try {
httpTransport.call(SOAP_ACTION, envelope);
System.out.println("2!!!!!!!!!!!!!!!!");
SoapObject result = (SoapObject)envelope.bodyIn;
System.out.println("1!!!!!!!!!!!!!!!!");
input.setText(result.getProperty(0).toString());
} catch (Exception e) {
input.setText(e.getMessage());
}
请用此替换上述部分,
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();
System.out.println(result.toString());
这会奏效。另请阅读此博客。它有一个工作代码。 http://www.insightforfuture.blogspot.com/2012/02/accessing-apache-axis2-web-service_24.html