android - 从 Android 客户端将参数传递到 Windows Azure 上托管的 VB.NET WS

标签 android vb.net web-services azure ksoap2

我正在努力将参数发送到 Windows Azure 上托管的 VB.NET WS(Web 服务)。获取一个简单的 hello world(无参数)工作正常,但由于某种原因,我在 Web 服务端的参数为空?

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

<OperationContract()>
Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String

功能实现(Service1.svc.vb):

Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String  Implements IService1.GetAddition
    Return number1 + number2
End Function

Android客户端代码:

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。对于其他字符串参数化函数,客户端不会接收到任何字符串。

我正在使用 ksoap2-android- assembly-2.6.2-jar-with-dependencies、Android 2.1 并在 Eclipse 3.7.2 中进行开发。 Web 服务是在 Visual Studios 2010 中构建的

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

谢谢

最佳答案

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

How to call a WCF service using ksoap2 on android?

喜欢:

 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"); 
 } 

关于android - 从 Android 客户端将参数传递到 Windows Azure 上托管的 VB.NET WS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10106252/

相关文章:

java - Android 地理围栏不触发 IntentService

c# - 使用 Infragistics UltraDateTimeEditor for WinForms 查看时间

VB.Net 线程

c# - 在 Windows Phone 异步服务中捕获服务器端异常

android - 水平 ScrollView 按钮焦点

android - CardView突然变黑

android - 如何在服务中启动新线程?

asp.net - Visual Studio 2013 中的 PageMethod 未调用(触发)WebMethod

web-services - 如何使用 postman 将请求传递给 sagemaker

ios - 为什么我的 Safari 推送通知的日志记录 Web 服务端点收到空白请求且没有错误消息