在Android中调用.NET web服务
我的.NETWeb服务返回一个XML。当我在Android中使用ksoap2调用web服务时,我遇到了这个问题。 请帮忙 我在Eclipse中的Logcat在Android中调用.NET web服务,android,web-services,android-layout,ksoap2,android-ksoap2,Android,Web Services,Android Layout,Ksoap2,Android Ksoap2,我的.NETWeb服务返回一个XML。当我在Android中使用ksoap2调用web服务时,我遇到了这个问题。 请帮忙 我在Eclipse中的Logcat 12-19 15:58:11.951: W/dalvikvm(2041): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 12-19 15:58:11.961: E/AndroidRuntime(2041): FATAL EXCEPTION: ma
12-19 15:58:11.951: W/dalvikvm(2041): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-19 15:58:11.961: E/AndroidRuntime(2041): FATAL EXCEPTION: main
12-19 15:58:11.961: E/AndroidRuntime(2041): java.lang.RuntimeException: double ID
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:407)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.Transport.parseResponse(Transport.java:116)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:259)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.example.webservicee.MainActivity$1.onClick(MainActivity.java:133)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View.performClick(View.java:2408)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View$PerformClick.run(View.java:8816)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.handleCallback(Handler.java:587)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Looper.loop(Looper.java:123)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invoke(Method.java:521)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
//your webservice envelope
String envelope="<?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><GetContentTreeBySections xmlns=\"http://tempuri.org/\"><SectionsInclude>%s</SectionsInclude><DocumentCount>%s</DocumentCount><SortBy>%s</SortBy><SortOrder>%s</SortOrder><UserName>%s</UserName><Pass>%s</Pass></GetContentTreeBySections></soap:Body></soap:Envelope>";
String myEnvelope = String.format(envelope, "myParam1 value", "myParam2 value","myParam3 value","myParam4 value");
String url = "http://www.mywebserice.com/XMLGenerator/GenerateXML.asmx";
String soapAction = "http://tempuri.org/GetContentTreeBySections";
String response = CallWebService(url, soapAction, myEnvelope);
//Log.v("response", response);
Toast.makeText(getApplicationContext(), "this"+response,Toast.LENGTH_LONG).show();
String xml=response;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
String CallWebService(String url,String soapAction,String envelope)
{
final DefaultHttpClient httpClient=new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString="";
try
{
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh=new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString=httpClient.execute(httppost, rh);
}
catch (Exception e) {
Log.v("exception", e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
MainActivity.java
12-19 15:58:11.951: W/dalvikvm(2041): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-19 15:58:11.961: E/AndroidRuntime(2041): FATAL EXCEPTION: main
12-19 15:58:11.961: E/AndroidRuntime(2041): java.lang.RuntimeException: double ID
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:407)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.Transport.parseResponse(Transport.java:116)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:259)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.example.webservicee.MainActivity$1.onClick(MainActivity.java:133)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View.performClick(View.java:2408)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View$PerformClick.run(View.java:8816)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.handleCallback(Handler.java:587)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Looper.loop(Looper.java:123)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invoke(Method.java:521)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
//your webservice envelope
String envelope="<?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><GetContentTreeBySections xmlns=\"http://tempuri.org/\"><SectionsInclude>%s</SectionsInclude><DocumentCount>%s</DocumentCount><SortBy>%s</SortBy><SortOrder>%s</SortOrder><UserName>%s</UserName><Pass>%s</Pass></GetContentTreeBySections></soap:Body></soap:Envelope>";
String myEnvelope = String.format(envelope, "myParam1 value", "myParam2 value","myParam3 value","myParam4 value");
String url = "http://www.mywebserice.com/XMLGenerator/GenerateXML.asmx";
String soapAction = "http://tempuri.org/GetContentTreeBySections";
String response = CallWebService(url, soapAction, myEnvelope);
//Log.v("response", response);
Toast.makeText(getApplicationContext(), "this"+response,Toast.LENGTH_LONG).show();
String xml=response;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
String CallWebService(String url,String soapAction,String envelope)
{
final DefaultHttpClient httpClient=new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString="";
try
{
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh=new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString=httpClient.execute(httppost, rh);
}
catch (Exception e) {
Log.v("exception", e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
package com.example.webservice;
导入java.io.IOException;
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.SoapFault;
导入org.ksoap2.serialization.PropertyInfo;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapPrimitive;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
导入org.xmlpull.v1.XmlPullParserException;
导入android.os.Bundle;
导入android.app.Activity;
导入android.util.Log;
导入android.view.Menu;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
最终静态字符串命名空间=”http://tempuri.org/";
最终静态字符串方法\u NAME=“GetContent”;
最终静态字符串SOAP_ACTION=”http://tempuri.org/GetContent";
最终静态字符串URL=”http://www.example.com/XMLGenerator/GenerateXML.asmx";
专用按钮Mybtn;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deger=(EditText)findViewById(R.id.deger);
sonuc=(TextView)findViewById(R.id.flag);
Mybtn=(按钮)findViewById(R.id.hesapla);
Mybtn.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
SoapObject请求=新的SoapObject(名称空间、方法名称);
//我的Web服务参数
请求。添加属性(“第条包括”、“10”);
请求。添加属性(“DocumentCount”,10);
请求。添加属性(“排序”、“0”);
请求。添加属性(“排序器”、“ASC”);
Request.addProperty(“用户名”、“我的用户”);
请求。添加属性(“通行证”、“Lsss”);
/**/
/*PropertyInfo pi=新的PropertyInfo();
pi.setName(“arg0”);
pi.设定值(“10”);
pi.setType(String.class);
请求。添加属性(pi);
pi=新属性info();
pi.setName(“arg1”);
pi.设定值(10);
pi.setType(Integer.class);
请求。添加属性(pi);
pi=新属性info();
pi.setName(“arg2”);
pi.设定值(“0”);
pi.setType(String.class);
请求。添加属性(pi);
pi=新属性info();
pi.setName(“arg3”);
pi.设定值(“ASC”);
pi.setType(String.class);
请求。添加属性(pi);
pi=新属性info();
pi.setName(“arg4”);
pi.设定值(“myuser”);
pi.setType(String.class);
请求。添加属性(pi);
pi=新属性info();
pi.setName(“arg5”);
pi.设定值(“Lsss”);
pi.setType(String.class);
请求。添加属性(pi);
*/
SoapSerializationEnvelope soapEnvelope=新的SoapSerializationEnvelope(soapEnvelope.VER11);
soapEnvelope.dotNet=true;
setOutputSoapObject(请求);
//soapEnvelope.bodyOut=请求;
HttpTransportSE aht=新的HttpTransportSE(URL);
试一试{
aht.call(SOAP\u动作,soapEnvelope);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}catch(XMLPullParseRexE){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串结果;
试一试{
结果=“华氏:”+(SoapPrimitive)soapEnvelope.getResponse();
Log.d(“--Result--”,Result);
//sonuc.SETEXT(结果);
}捕获(SOAPE)
{
e、 printStackTrace();
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}
以下是响应xml的示例:
<?xml version="1.0" encoding="utf-8"?>
<liste>
<document id="6" titles="bestPeople" showingDate="27.09.2011 00:00:00" date="27.09.2011 00:00:00" ContentID=rbHe8zKvDTG7TLS" >
<part id="6" baslik="best" icerik="<table style="width: 188px; height: 24px;" border="tbody>
</table>" resim="">
<SecNotes><![CDATA[]]></SecNotes>
<ContNotes><![CDATA[]]></ContNotes>
</part>
<SecNotes><![CDATA[]]></SecNotes>
<ContNotes><![CDATA[]]></ContNotes>
</document>
</liste>
不要使用ksoap,而是尝试使用开源的ICESOAP库。
它比ksoap好得多,并提供了使用注释发送请求和解析响应的高效库。…首先,不要在主线程中使用网络操作。请始终尝试在非主线程(如AsyncTask或任何其他工作线程)中插入网络操作。 其次,使用以下代码获得结果:
String message="";
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
message=result.getProperty(0).toString();
}
并更改以下代码
Request.addProperty("DocumentCount",10);
将来
Request.addProperty("DocumentCount","10");
我解决它。我添加了这个代码。它起作用了 我的代码
12-19 15:58:11.951: W/dalvikvm(2041): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-19 15:58:11.961: E/AndroidRuntime(2041): FATAL EXCEPTION: main
12-19 15:58:11.961: E/AndroidRuntime(2041): java.lang.RuntimeException: double ID
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:407)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.Transport.parseResponse(Transport.java:116)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:259)
12-19 15:58:11.961: E/AndroidRuntime(2041): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.example.webservicee.MainActivity$1.onClick(MainActivity.java:133)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View.performClick(View.java:2408)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.view.View$PerformClick.run(View.java:8816)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.handleCallback(Handler.java:587)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.os.Looper.loop(Looper.java:123)
12-19 15:58:11.961: E/AndroidRuntime(2041): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:58:11.961: E/AndroidRuntime(2041): at java.lang.reflect.Method.invoke(Method.java:521)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-19 15:58:11.961: E/AndroidRuntime(2041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
//your webservice envelope
String envelope="<?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><GetContentTreeBySections xmlns=\"http://tempuri.org/\"><SectionsInclude>%s</SectionsInclude><DocumentCount>%s</DocumentCount><SortBy>%s</SortBy><SortOrder>%s</SortOrder><UserName>%s</UserName><Pass>%s</Pass></GetContentTreeBySections></soap:Body></soap:Envelope>";
String myEnvelope = String.format(envelope, "myParam1 value", "myParam2 value","myParam3 value","myParam4 value");
String url = "http://www.mywebserice.com/XMLGenerator/GenerateXML.asmx";
String soapAction = "http://tempuri.org/GetContentTreeBySections";
String response = CallWebService(url, soapAction, myEnvelope);
//Log.v("response", response);
Toast.makeText(getApplicationContext(), "this"+response,Toast.LENGTH_LONG).show();
String xml=response;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
String CallWebService(String url,String soapAction,String envelope)
{
final DefaultHttpClient httpClient=new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString="";
try
{
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh=new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString=httpClient.execute(httppost, rh);
}
catch (Exception e) {
Log.v("exception", e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
私有void init()
{
//您的Web服务信封
字符串信封=“%s%s%s%s%s%s”;
String myEnvelope=String.format(信封,“myParam1值”、“myParam2值”、“myParam3值”、“myPar