【问题标题】:android app is not sending value to web serviceandroid应用程序没有向网络服务发送价值
【发布时间】:2017-07-21 18:06:31
【问题描述】:

我的 webservicecall.java 文件包含调用服务器的代码

  public class WebserviceCall {

/**
 * Variable Decleration................
 *
 */
String namespace = "http://bkagartala.technotripura.com/";
private String url = "http://bkagartala.technotripura.com/asha/WebService.asmx";

String SOAP_ACTION;
SoapObject request = null, objMessages = null;
SoapSerializationEnvelope envelope;
HttpTransportSE 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 HttpTransportSE(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 getchange_liveserver(String MethodName, String f_name)
{

    try {
        SOAP_ACTION = "http://tempuri.org/"+MethodName;

        //Adding values to request object
        request = new SoapObject(namespace, MethodName);

        //Adding Double value to request object
        PropertyInfo weightProp =new PropertyInfo();
        weightProp.setName("f_name");
        weightProp.setValue(f_name);

        weightProp.setType(String.class);

         request.addProperty(weightProp);

        //Adding String value to request object

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

   }


/************************************/
 }

在 asynctask 中将该 Web 服务类调用到另一个活动

      aResponse = com.getchange_liveserver("soumya", "one");

但主要问题应用程序没有将该值发送到服务器......

网络服务代码

 Imports System.Web
  Imports System.Web.Services
   Imports System.Web.Services.Protocols



  <WebService(Namespace:="http://tempuri.org/")> _
 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  Public Class WebService
 Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function HelloWorld() As String
    Return "Hello World"
End Function
<WebMethod()> _
Public Function soumya(ByVal f_name As String) As String


    Dim a As String = f_name


    If a = "one" Then
        a = "hello"
        Return a

    ElseIf a = "two" Then
        a = "may"
        Return a
    Else
        a = "do"
        Return a


    End If
End Function

   End Class

【问题讨论】:

  • 发布您遇到的错误或异常
  • 我没有得到任何错误只是没有得到所需的输出。
  • 它应该给我“你好”
  • 问题是它正在向 Web 服务发送空值

标签: android web-services android-asynctask


【解决方案1】:

将您的命名空间更改为 tempuri.org 我想这就是问题所在...我已经给出了简单的示例尝试并根据您的值替换...

我想问题出在你的 NAMESPACE 我可以在你的服务文件中看到 tempuri.org

@Override
    protected Void doInBackground(Void... voids) {
        try {
            String NAMESPACE = "http://tempuri.org/";
            String METHOD_NAME = "Your Method Name";
            String SOAP_ACTION = NAMESPACE+METHOD_NAME;
            String URL = "http://bkagartala.technotripura.com/asha/WebService.asmx";
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("methodName", METHOD_NAME);

            request.addProperty("ModelNumber", VariableName You Want to Pass);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

【讨论】:

  • I/AndroidExampleOutput: ----java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'java.lang.String java.lang.Object.toString()'跨度>
  • 在哪一行?..有错误然后没有错误也很好......你至少可以纠正它们;)
  • request = new SoapObject(namespace, MethodName); //向请求对象PropertyInfo添加Double值 weightProp =new PropertyInfo(); weightProp.setName("f_name"); weightProp.setValue(f_name); weightProp.setType(String.class); request.addProperty(weightProp);
  • 那么不要添加双精度值...使用 String.valueOf(variable having double value) 还可以更改服务文件中该变量的接受类型
  • 试过这种方式... request.addProperty("methodName", "soumya"); request.addProperty("f_name", f_name);但同样的空指针异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
相关资源
最近更新 更多