如何将参数从Android应用程序传递到asp.net C#webservice以及如何在asp.net中处理该参数? [英] how to passing a parameter from android app to asp.net C# webservice and how to handlem that parameter in asp.net?
问题描述
package com.example.dotnwebservice;
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.AndroidHttpTransport;
公共类WebserviceCall {
// String namespace =http://www.webserviceX.NET/;
String namespace =http://tempuri.org/ ConvertWeight /;
// private String url =http://www.webservicex.net/ConvertWeight.asmx;
// private String url =http ://192.168.1.10/Ws/Service1.asmx;
private String url =http://192.168.1.3/vs/Service1.asmx;
String SOAP_ACTION;
SoapObject request = null,objMessages = null;
SoapSerializationEnvelope信封;
AndroidHttpTransport androidHttpTransport;
WebserviceCall()
{
}
/ **
*设置信封
* /
protected void SetEnvelope(){
try {
//创建SOAP信封
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//如果您的网络服务不是.NET服务,您可以评论该行。
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
androidHttpTransport = new AndroidHttpTransport( url);
androidHttpTransport.debug = true;
} catch(例外e){
系统。 out.println(Soap Exception ---->>>+ e.toString());
}
}
// MethodName变量定义为哪个webservice函数调用
public String ConvertWeight(String MethodName,String weight,
String fromUnit,String toUnit)
{
try {
SOAP_ACTION = namespace + MethodName;
//为请求对象添加值
request = new SoapObject(namespace,MethodName);
//向请求对象添加Double值
PropertyInfo weightProp = new PropertyInfo();
weightProp.setName(Weight);
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);
//将字符串值添加到请求对象
request.addProperty( FromUnit,+ fromUnit);
request.addProperty(ToUnit ,+ toUnit);
SetEnvelope();
试试{
// SOAP调用webservice
androidHttpTransport.call(SOAP_ACTION,信封);
//得到Webservice响应
字符串结果= envelope.getResponse()。toString();
返回结果;
} catch(例外e){
// TODO:处理异常
返回e.toString();
}
} catch(例外e){
// TODO:处理异常
返回e.toString();
}
}
/ ******************************* ***** /
}
这是我的用于调用Web服务的android代码
和
。
package com.example.dotnwebservice;
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.AndroidHttpTransport;
public class WebserviceCall {
//String namespace = "http://www.webserviceX.NET/";
String namespace = "http://tempuri.org/ConvertWeight/";
//private String url = "http://www.webservicex.net/ConvertWeight.asmx";
//private String url = "http://192.168.1.10/Ws/Service1.asmx";
private String url = "http://192.168.1.3/vs/Service1.asmx";
String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
AndroidHttpTransport androidHttpTransport;
WebserviceCall()
{
}
/**
* Set Envelope
*/
protected void SetEnvelope() {
try {
// Creating SOAP envelope
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//You can comment that line if your web service is not .NET one.
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
androidHttpTransport = new AndroidHttpTransport(url);
androidHttpTransport.debug = true;
} catch (Exception e) {
System.out.println("Soap Exception---->>>" + e.toString());
}
}
// MethodName variable is define for which webservice function will call
public String ConvertWeight(String MethodName, String weight,
String fromUnit, String toUnit)
{
try {
SOAP_ACTION = namespace + MethodName;
//Adding values to request object
request = new SoapObject(namespace, MethodName);
//Adding Double value to request object
PropertyInfo weightProp =new PropertyInfo();
weightProp.setName("Weight");
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);
//Adding String value to request object
request.addProperty("FromUnit", "" + fromUnit);
request.addProperty("ToUnit", "" + toUnit);
SetEnvelope();
try {
//SOAP calling webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
//Got Webservice response
String result = envelope.getResponse().toString();
return result;
} catch (Exception e) {
// TODO: handle exception
return e.toString();
}
} catch (Exception e) {
// TODO: handle exception
return e.toString();
}
}
/************************************/
}
here is my android code for calling web service
and
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
namespace AndroidDemoService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/ConvertWeight")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ConvertWeight(String MethodName, String weight, String fromUnit, String toUnit)
{
if (weight == "" || weight == null)
{
string SA = "No Weight Received";
return SA;
}
else
{
int W1 = Convert.ToInt32(weight.ToString());
int weight1 = W1 / 1000;
string SA = weight1.ToString();
return weight;
}
//string SA = "hey this is test from nikunj.";
}
}
}
以上是我的asp.net服务代码我能在Android上获得这个web服务的结果
但是我传递参数从我的应用程序到服务网络服务无法给出结果。
这里在我的网络服务中
只有当其中一部分工作时才会生效当我传递参数
above is my asp.net service code i m able to getting result of this web service in android
but wen i passing parameter from my app to service web service could not giving a result.
here in my webservice
only if part of that is working but else part is not working when i am passing parameters
推荐答案
我发现网络服务名称空间有问题时,部分无法工作
[WebService(Namespace =http://tempuri.org/ConvertWeight)]这个的实例
使用[ WebService(Namespace =http://tempuri.org/ConvertWeight/)]
i find a problem that is in name space in webservice
[WebService(Namespace = "http://tempuri.org/ConvertWeight")] instade of this
use [WebService(Namespace = "http://tempuri.org/ConvertWeight/")]
这篇关于如何将参数从Android应用程序传递到asp.net C#webservice以及如何在asp.net中处理该参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!