从Android发送SOAP请求。服务器响应:";服务器无法处理请求&引用;

从Android发送SOAP请求。服务器响应:";服务器无法处理请求&引用;,android,soap,request,Android,Soap,Request,我尝试向w3schools服务器发送Android的SOAP查询请求,在该服务器上,它将温度从摄氏度转换为华氏度,但我从服务器收到以下响应: Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1. 施维雅在: . 以及我们可以在上找到的SOAP请求消息结构:。 这是我的密码 public class MainActivity exte

我尝试向w3schools服务器发送Android的SOAP查询请求,在该服务器上,它将温度从摄氏度转换为华氏度,但我从服务器收到以下响应:

Server was unable to process request. 
---> Data at the root level is invalid. 
Line 1, position 1. 
施维雅在: . 以及我们可以在上找到的SOAP请求消息结构:。 这是我的密码

public class MainActivity extends Activity {

private static final String URL =           
           "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   HttpClient httpclient = new DefaultHttpClient();
   HttpResponse response;
   StringBuffer sb = new StringBuffer("" +
      "POST /webservices/tempconvert.asmx HTTP/1.1" +
      "Host: w3schools.com" +
      //"Content-Type: text/xml; charset=utf-8" +
      //"Content-Length: length" +
      "SOAPAction: \"http://tempuri.org/CelsiusToFahrenheit\"" +
          "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
      "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                     "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
             "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
     "<soap:Body>" +
       "<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">" +
           "<Celsius>24</Celsius>" +
       "</CelsiusToFahrenheit>" +
     "</soap:Body>" +
       "</soap:Envelope>");

   HttpPost request = new HttpPost(URL);
   request.setHeader("Content-Type", "application/soap+xml; charset=utf-8");

   tv = (TextView) findViewById(R.id.textView1);

   try {
     List<NameValuePair> pairs = new ArrayList<NameValuePair>();
     pairs.add(new BasicNameValuePair("myxml", sb.toString()));
     request.setEntity(new UrlEncodedFormEntity(pairs));

     response = httpclient.execute(request);
     String response_string = EntityUtils.toString(response.getEntity());
     //Here we should parse the response xml
     //But for quicker way, we will display the whole response.
     tv.setText("Temperature in Fahrenheit is: " + response_string);
     Log.v("responseString", response_string)

   } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
   } catch (ClientProtocolException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }
    }
 }
公共类MainActivity扩展活动{
私有静态最终字符串URL=
"http://www.w3schools.com/webservices/tempconvert.asmx";
文本视图电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse响应;
StringBuffer sb=新的StringBuffer(“”)+
“POST/webservices/tempconvert.asmx HTTP/1.1”+
“主持人:w3schools.com”+
//“内容类型:text/xml;字符集=utf-8”+
//“内容长度:长度”+
“SOAPAction:\”http://tempuri.org/CelsiusToFahrenheit\"" +
"" +
"" +
"" +
"" +
"24" +
"" +
"" +
"");
HttpPost请求=新的HttpPost(URL);
setHeader(“内容类型”,“应用程序/soap+xml;字符集=utf-8”);
tv=(TextView)findViewById(R.id.textView1);
试一试{
列表对=新的ArrayList();
add(新的BasicNameValuePair(“myxml”,sb.toString());
setEntity(新的UrlEncodedFormEntity(对));
response=httpclient.execute(请求);
String response_String=EntityUtils.toString(response.getEntity());
//这里我们应该解析响应xml
//但为了更快,我们将显示整个响应。
tv.setText(“华氏温度为:“+响应字符串”);
Log.v(“响应字符串”,响应字符串)
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
如果你们能帮我,我将不胜感激。
谢谢大家!

< P>你可能想考虑使用这个。 特别是关于解决这个问题,我使用SoapUI进行了一些测试,以检查WS,并使用编写请求的方式进行了一些测试,事实上,它给出了一个错误。尝试将tempuri.org添加到信封:

  "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
         "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "+ 
         "xmlns:tem=\"http://tempuri.org/\"   >  " +
“”+
这样写正文:

   <soap:Body>
      <tem:CelsiusToFahrenheit>
         <tem:Celsius>30</tem:Celsius>
      </tem:CelsiusToFahrenheit>
   </soap:Body>

30

希望这有帮助,并考虑使用,真正的

。如果有帮助,请检查这个。@Raghunandan。谢谢Raghunandan,我第一次尝试使用KSOAP2,但是.jars code.google.com提供的不是很好。它们要么只适合于没有定义类的编译,要么根本就不好。我在com.example.soap\u ksoap2\u lino.MainActivity.onCreate(MainActivity.java:29)上得到java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject。也许,你能给我一个运行良好的ksoap2 android…jar吗?确保你将jar添加到项目的libs文件夹中,该文件夹可以工作是的,我已经将它添加到构建路径中。.jar根本就不好。我不知道我用过ksoaop2 jar,它对我有用。谷歌它和下载最新的一个它应该工作Hi SysHex。非常感谢你。我确实是第一次使用ksoap2,但是code.google.com提供的.jar并不好。它们要么只适合于没有定义类的编译,要么根本就不好。我在com.example.soap\u ksoap2\u lino.MainActivity.onCreate(MainActivity.java:29)上得到java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject,你能给我发一个运行良好的ksoap2 android…jar吗?嗨!我现在用的是。到那儿去。非常感谢你!