代理服务器后面的Android Soap Web服务错误
我编写了一个程序,用于与web服务通信并获取响应值。但是,当我调试程序时,我在代理服务器后面的Android Soap Web服务错误,android,web-services,android-ksoap2,Android,Web Services,Android Ksoap2,我编写了一个程序,用于与web服务通信并获取响应值。但是,当我调试程序时,我在androidHttpTransport.call(SOAP\u操作,信封)行以requestDump=null结束
androidHttpTransport.call(SOAP\u操作,信封)行以requestDump=null结束有人能告诉我错误的原因吗?我能做些什么
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}
公共类Web服务扩展活动{
私有最终字符串命名空间=”http://tempuri.org/";
私有最终字符串URL=”http://www.w3schools.com/webservices/tempconvert.asmx";
私有最终字符串SOAP_ACTION=”http://tempuri.org/CelsiusToFahrenheit";
私有最终字符串方法\u NAME=“CelsiusToFahrenheit”;
字符串摄氏度;
按钮b;
文本视图电视;
编辑文本;
字符串res,resultval;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u web\u服务);
et=(EditText)findViewById(R.id.editText1);
tv=(文本视图)findViewById(R.id.Result);
b=(按钮)findViewById(R.id.button1);
b、 setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
//字符串结果=getFarenheit(et.getText().toString());
//tv.setText(结果+“°F”);
新服务().execute();
}
});
}
私有类服务扩展异步任务{
@凌驾
受保护的字符串doInBackground(无效…arg0){
摄氏度=et.getText().toString();
SoapObject请求=新的SoapObject(名称空间、方法名称);
PropertyInfo celsuiupi=新的PropertyInfo();
celsuiusPI.setName(“摄氏度”);
celsuiusPI.设定值(摄氏度);
setType(String.class);
请求。添加属性(“XMLMarks”,celsuiusPI);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes=true;
envelope.enc=SoapSerializationEnvelope.ENC2003;
envelope.xsd=SoapEnvelope.xsd;
envelope.xsi=SoapEnvelope.xsi;
envelope.setOutputSoapObject(请求);
信封.装饰(假);
原始反应;
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
试一试{
androidHttpTransport.setXmlVersionTag(“”);
androidHttpTransport.debug=true;
调用(SOAP_操作,信封);
字符串转储=androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this,response.toString(),20.show();
Log.i(“WebService输出”,response.toString());
System.out.println(“WebService响应”+Response.toString());
Object res=response.toString();
resultval=(字符串)res;
}
捕获(例外e){
e、 printStackTrace();
}
返回res;
}
受保护的void onPostExecute(字符串h){
字符串结果=h;
tv.setText(结果+“°F”);
}
}
}
只需将您的服务任务替换为新任务,即可看到结果:
代码:
私有类服务扩展异步任务{
@凌驾
受保护的字符串doInBackground(无效…arg0){
System.out.println(“In-DoIn后台”);
//初始化soap请求+添加参数
SoapObject请求=新的SoapObject(名称空间、方法名称);
//使用此选项可以添加参数
request.addProperty(“摄氏度”,txtCel.getText().toString());
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第11版);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
//这是将调用webservice的实际部分
调用(SOAP_操作,信封);
//从信封正文获取SoapResult。
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(结果!=null){
//获取第一个属性并更改标签文本
//setText(result.getProperty(0.toString());
res=result.getProperty(0.toString();
}否则{
Toast.makeText(getApplicationContext(),“无响应”,
Toast.LENGTH_LONG).show();
}
}捕获(例外e){
e、 printStackTrace();
}
返回res;
}
受保护的void onPostExecute(字符串h){
字符串结果=h;
tv.setText(结果+“°F”);
}
}
如何使用androidHttpTransport.setXmlVersionTag(“”);没有必要这样做。肥皂只是给你一次机会。您不必从xml结构解析它。但我仍然得到null'F,结果是代理请求转储上的iam为null。如何将代理添加到HttpTransportsE您是否只是复制粘贴上面的代码并运行项目,或者在应用程序中做了更多的工作??因为我可以在这里成功地运行它。所以希望你们得到答案。你也可以投票表决。所以它可以帮助其他人。复制粘贴的项目,但仍然以同样的错误结束,如何给HTTPTransportSEiDroid浏览器代理你有什么解决方案吗
private class service extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.getText().toString());
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}