我是ksoap2的新手,我正试图让w3学校的例子正常运作。由于某种原因,它总是在这条线上失败。
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
我不知道导致失败的原因。 Android应用程序爆炸了。我在Manifest中设置了互联网权限。我不确定发生了什么。感谢
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class WebServiceTurorialActivity extends Activity implements OnClickListener{
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
(findViewById(R.id.button1)).setOnClickListener(this);
}
public void onClick(View v) {
int a;
int b;
try
{
// EditText ed1=(EditText)findViewById(R.id.editText1);
// EditText ed2=(EditText)findViewById(R.id.editText2);
// a = Integer.parseInt(ed1.getText().toString());
// b = Integer.parseInt(ed2.getText().toString());
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
//Request.addProperty("a", a);
// Request.addProperty("b", b);
Request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
Toast.makeText(this,"200 " + resultString,Toast.LENGTH_SHORT).show();
}catch(Exception ex) {
Toast.makeText(this,"FALSE",Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:4)
我遇到了同样的问题。这对我有用:
正如前面的回答所说:
不适合我。我的方式:
答案 1 :(得分:3)
对于每个人也有这个问题...我试图解决这个问题很多个小时,但后来我发现了我的错误:我没有正确下载ksoap2库,所以只有一个jar文件是21 KB,但正确的jar文件大约是150KB。因此,请确保正确下载文件!
答案 2 :(得分:1)
如果您仍然遇到同样的问题,请尝试这些课程(适合我):
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String fahrenheit = CelsiusToFahrenheitWs.celsiusToFahrenheit("32");
Toast.makeText(getApplicationContext(), fahrenheit, Toast.LENGTH_SHORT).show();
}
}
和
public class CelsiusToFahrenheitWs {
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
public static String celsiusToFahrenheit(String celsius) {
String fahrenheit = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", celsius);
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;
}
}
答案 3 :(得分:1)
我在Android上创建了另一个SOAP库来调用SOAP服务,项目名称是AndroidSOAP(http://wiki.javaforum.hu/display/ANDROIDSOAP/Home)。
我今天发布了0.0.5版本,请查看:http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/05/01/Version+0.0.5+released
答案 4 :(得分:0)
W3Schools.com不再接受NAMESPACE作为tempuri.org,导致代码中出现SOAP异常。您需要对NAMESPACE使用“http://www.w3schools.com/webservices/”,对SOAP_ACTION使用“http://www.w3schools.com/webservices/CelsiusToFahrenheit”。
希望这是有帮助的!