我试图从我的Android应用程序调用Web服务,但是我收到了这个错误: SoapFault - faultcode:' soap:Client' faultstring:'服务器无法识别HTTP标头SOAPAction的值:http://tempuri.org/CelsiusToFahrenheit。' faultactor:' null'详情:org.kxml2.kdom.Node@41b9f3a0
我的代码如下:
package com.example.test;
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.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity 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";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
new LongRunningAsyncTask().execute("");
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class LongRunningAsyncTask extends AsyncTask<String, String, String> {
String currentText;
@Override
protected String doInBackground(String... params) {
currentText = getFahrenheit("10");
return null;
}
@Override
protected void onPreExecute() {
currentText = "Start: ";
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("End: " + currentText);
super.onPostExecute(result);
}
}
public String getFahrenheit(String celsius) {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
PropertyInfo celsiusPI = new PropertyInfo();
//Set Name
celsiusPI.setName("Celsius");
//Set Value
celsiusPI.setValue(celsius);
//Set dataType
celsiusPI.setType(double.class);
//Add the property to request object
request.addProperty(celsiusPI);
//Create envelope
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
//Invole web service
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
return response.toString();
} catch (Exception e) {
e.printStackTrace();
String error = e.getMessage();
return error;
}
}
}
错误在这一行: SoapPrimitive response =(SoapPrimitive)envelope.getResponse();
我认为调用函数无法正常运行,但调试器在获取响应时会出错:envelope.getResponse