首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从安卓客户端向Windows Azure上托管的VB.NET WS传递参数

从安卓客户端向Windows Azure上托管的VB.NET WS传递参数
EN

Stack Overflow用户
提问于 2012-04-11 20:50:54
回答 1查看 516关注 0票数 2

我正在努力将参数发送到托管在Windows Azure上的VB.NET WS(web服务)。获取一个简单的hello world (没有参数)很好,但是由于某些原因,我在web服务端的参数是空的?

要调用的Web服务函数(IService1.vb):

代码语言:javascript
复制
<OperationContract()>
Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String

函数实现(Service1.svc.vb):

代码语言:javascript
复制
Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String  Implements IService1.GetAddition
    Return number1 + number2
End Function

Android客户端代码:

代码语言:javascript
复制
import java.util.ArrayList;

import android.app.*;
import android.os.*;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class Test2Activity extends Activity {
TextView tv;

private static final String SOAP_ACTION = "http://tempuri.org/IService1/";    
private static final String NAMESPACE = "http://tempuri.org/";    
private static final String URL = "http://a42c90a9e3e74fffa7b0093001f51de8.cloudapp.net/Service1.svc";    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    System.setProperty("http.keepAlive", "false");
    tv=(TextView)findViewById(R.id.textView1);               

    ArrayList<Object> tmpParam = new ArrayList<Object>();
    tmpParam.add(1);
    tmpParam.add(4);

    String result = call("GetAddition", tmpParam, new String[]{"number1","number2"});
    //String result = call("GetHello", new ArrayList<Object>(), new String[]{""}); //works

    tv.setText(result);


    }

public String call(String methodName, ArrayList<Object> arrParameterValues, String arrParameterNames[])
{
    try {

        SoapObject request = new SoapObject(NAMESPACE, methodName);

        for (int k = 0;k < arrParameterValues.size() ; k++){
            PropertyInfo tmp = new PropertyInfo();
            tmp.setName(arrParameterNames[k]);
            tmp.setValue(arrParameterValues.get(k));
            tmp.setType(int.class); //hard coded for now
            request.addProperty(tmp);
        }
        //tried
        /*
        request.addProperty("number1",1);
        request.addProperty("number2",1);
        */

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION.concat(methodName) , envelope);

        //If an XmlPullParserException occurs, retry once in order to workaround an Android emulator bug
        try {
            androidHttpTransport.call(SOAP_ACTION.concat(methodName), envelope);
        } 
        catch(XmlPullParserException ex) {
            ex.printStackTrace();

            androidHttpTransport.call(SOAP_ACTION.concat(methodName), envelope);
        }

        Object result = envelope.getResponse();                 
        return result.toString();
    }
    catch(Exception ex) {
        return ex.toString();
    }
}   
}//end of class    

返回值为0。使用其他字符串参数化函数时,客户端上不会收到任何字符串。

我使用的是EclipseAndroid2.1,开发环境是ksoap2-android-assembly-2.6.2-jar-with-dependencies,3.7.2。web服务是在Visual Studios 2010中构建的

你们中的一些人可能会想:“为什么是VB而不是C#?”原因是我已经有了一定的VB经验:)

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-12 03:38:47

您是否可以使用如下问题中的方法传递参数:

How to call a WCF service using ksoap2 on android?

像这样:

代码语言:javascript
复制
 try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("Name", "Qing"); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
            SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.call(SOAP_ACTION, envelope); 
    SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 

    //to get the data 
    String resultData = result.toString(); 
    // 0 is the first object of data  


    sb.append(resultData + "\n"); 
 } catch (Exception e) { 
    sb.append("Error:\n" + e.getMessage() + "\n"); 
 } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10106252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档