Java 从Android调用Web服务时获取异常
我是这方面的初学者,在从android调用asp.NETWeb服务方法(注册)时遇到了一个问题。当我在浏览器上尝试时,它工作得很好,但当我在android上尝试时,它会给我一些例外,比如 " SoapFault-faultcode:'soap:Client'faultstring:'System.Web.Services.Protocols.SoapException:服务器未识别HTTP标头SOAPAction:SignUp的值。 在System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouterRequest()中 在System.Web.Services.Protocols.SoapServerProtocol.Initialize()中 位于System.Web.Services.Protocols.ServerProtocol.SetContext(类型类型、HttpContext上下文、HttpRequest请求、HttpResponse响应) 在System.Web.Services.protocol.ServerProtocolFactory.Create(类型、HttpContext上下文、HttpRequest请求、HttpResponse响应、Boolean&abortProcessing)中的faultactor:'null'详细信息:org.kxml2.kdom 这是我的Java注册类Java 从Android调用Web服务时获取异常,java,android,asp.net,web-services,Java,Android,Asp.net,Web Services,我是这方面的初学者,在从android调用asp.NETWeb服务方法(注册)时遇到了一个问题。当我在浏览器上尝试时,它工作得很好,但当我在android上尝试时,它会给我一些例外,比如 " SoapFault-faultcode:'soap:Client'faultstring:'System.Web.Services.Protocols.SoapException:服务器未识别HTTP标头SOAPAction:SignUp的值。 在System.Web.Services.Protocols.
String SOAP_ACTION1 = "http://tempuri.org/SignUp";
String METHOD_NAME1 = "SignUp";
btn = (Button)findViewById(R.id.btn_signup);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new WebService().execute(METHOD_NAME1,SOAP_ACTION1,txt_name.getText().toString(), txt_email.getText().toString(),txt_password.getText().toString(),
txt_address.getText().toString(),
txt_dob.getText().toString(),
txt_account.getText().toString(),
txt_cnic.getText().toString(),txt_pobox.getText().toString(),
txt_city.getText().toString(),txt_status.getText().toString());
这是我的JavaWebService类
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://192.168.0.102/MyWebService/WebService1.asmx?WSDL";
protected String doInBackground(String... params) {
String s="";
String whichmethodcall = params[0];
switch(whichmethodcall){
case "SignUp":
s = signup(params);
break;
case "SignIn":
s = signin(params);
break;
}
return s;
}
public String signup(String[] arr){
String s = "";
SoapObject request = new SoapObject(NAMESPACE, arr[1]);
//Use this to add parameters
request.addProperty("cname",arr[2]);
request.addProperty("cemail",arr[3]);
request.addProperty("cpwd",arr[4]);
request.addProperty("caddress",arr[5]);
request.addProperty("cdob",arr[6]);
request.addProperty("caccount",arr[7]);
request.addProperty("cCnic",arr[8]);
request.addProperty("cpobox",arr[9]);
request.addProperty("cCity",arr[10]);
request.addProperty("cstatus",arr[11]);
//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(arr[0], envelope);
// Get the SoapResult from the envelope body.
// SoapObject result = (SoapObject)envelope.bodyIn;
// SoapObject result = (SoapObject) envelope.getResponse();
if (envelope.bodyIn instanceof SoapFault) {
final SoapFault result = (SoapFault) envelope.bodyIn;
if (result != null) {
//Get the first property and change the label text
s = result.toString();
} else
s = "No response";
}
} catch (Exception e) {
e.printStackTrace();
s=e.getMessage().toString();
}
return s;
}
这是我的asp.NETWebService方法
public string SignUp(string cname, string caddress, string cdob, long caccount, long cCnic, int cpobox, string cCity, string cstatus,string cemail, string cpass)
{
bool check = ConnectDB(); // Opens the Connection to Insert The new User
string msg = "";
//DateTime dob = DateTime.ParseExact(cdob, "dd/mm/yyyy", null);
string query = "INSERT INTO Customers values('" + cname + "','" + caddress + "','" + cdob + "','" + caccount + "','" + cCnic + "','" + cpobox + "','" + cCity + "','" + cstatus + "','"+ cemail + "','"+ cpass + "')";
SqlCommand comm = new SqlCommand(query, con);
try{
if (check == true){
if (comm.ExecuteNonQuery() > 0)
msg = "you've signed up !";
else
msg = "sign up error";
}
else
msg = "Database not Connected";
}catch (SqlException ex){
msg = ex.Message.ToString();
con.Close();
}finally{
con.Close();
}
return msg;
}
你能试试这个吗
而不是SoapObject请求=新的SoapObject(名称空间,arr[1]);
使用
SoapObject请求=新的SoapObject(命名空间,arr[0])
在soap对象中,应该只传递方法名,而不传递http://...看起来您正在使用ksoap2。传输有两个字段requestDump和responseDump,这对调试非常有用。您能在最后一个子句(transport.requestDump!=null){Log.d(getClass().getName(),methodId+“”+transport.requestDump);}中添加类似的内容吗{Log.d(getClass().getName(),“+methodId”无请求转储);}如果(transport.responseDump!=null){Log.d(getClass().getName(),methodId+“”+transport.responseDump);}else{Log.d(getClass().getName(),“+methodId”无响应转储);}arr[2]包含custname。我应该在其中传递方法名。否?是的,刚刚看到..没有任何更改,我仍然得到相同的异常查看我在原始消息下的注释。尝试打印requestDump和ResponseDump忘记提及您必须添加androidHttpTransport.debug=true创建传输后另一个问题我看到参数不匹配您d“cpwd”,但服务器需要“cpass”