【问题标题】:(JAVA) SOAP WebService error object reference not set to an instance of an object(JAVA) SOAP WebService 错误对象引用未设置为对象的实例
【发布时间】:2014-10-07 21:52:27
【问题描述】:

我正在尝试从 Android 应用程序设置到 SOAP WebService 的连接,但每次我在结果中遇到错误时: 对象引用未设置为对象 java 的实例 似乎是来自服务器的错误 --> SOAP Webservice call from Java gives "Object reference not set to an instance of an object"

但是当我通过带有 POST 请求的网络浏览器尝试它时,它工作正常:)

此服务http://ws.cdyne.com/ip2geo/ip2geo.asmx?op=ResolveIP

private static String NAMESPACE = "http://ws.cdyne.com/";
private static String URL = "http://ws.cdyne.com/ip2geo/ip2geo.asmx";
private static String SOAP_ACTION = "http://ws.cdyne.com/";

public static String invokeHelloWorldWS(String name, String webMethName) {
    String resTxt = null;

    SoapObject request = new SoapObject(NAMESPACE, webMethName);
    PropertyInfo sayHelloPI = new PropertyInfo();
    // Set name
    sayHelloPI.setName("ipAddress");
    // Set Value
    sayHelloPI.setValue("88.212.35.129");
    // Set dataType
    sayHelloPI.setType(String.class);
    // Add the property to request object
    request.addProperty(sayHelloPI);
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;  
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
    try{
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);  //webMethName = "ResolveIP"
        // Get the response
        Log.d("a", androidHttpTransport.responseDump);
        //SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to resTxt variable static variable
        //resTxt = response.toString();
    }catch(Exception e){
         //Print error
        e.printStackTrace();
    }
}

我在谷歌上花了很多时间,但我无法正确回答为什么会发生这种情况

// 编辑 最后我做对了......我知道为什么但是当我发送这样的第二个参数时(我重用旧属性):

sayHelloPI.setName("licenseKey");
sayHelloPI.setValue("some_key");
sayHelloPI.setType(String.class);
request.addProperty(sayHelloPI);

它没有工作。但是当我创建新的 Property 对象时它可以工作:

PropertyInfo sayHelloPI1 = new PropertyInfo();
sayHelloPI1.setName("licenseKey");
sayHelloPI1.setValue("ds");
sayHelloPI1.setType(String.class);
request.addProperty(sayHelloPI1);

也许下次能帮到别人

【问题讨论】:

  • 你在使用 ksoap2 吗?
  • 是的,我的朋友。我知道我可以简单地从 android 发送 POST 请求(因为从浏览器,这种方式有效),我应该可以,但我想像普通的肥皂信封一样发送它

标签: java android web-services soap


【解决方案1】:

这是我自己使用的一些代码-希望对您有所帮助:

        // Initialize soap request + add parameters
        SoapObject request = new SoapObject(getString(R.string.Namespace),
                getString(R.string.Method_Name_GetStudentsByTeam));
        Log.d("GetStudentsByTeamTask", "SOAP request");

        // Use this to add parameters
        request.addProperty("teamId", params[0]);
        Log.d("GetStudentsByTeamTask", "id: " + params[0]);

        // Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        Log.d("GetStudentsByTeamTask",
                "Declared the version of the SOAP request");

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;
        Log.d("GetStudentsByTeamTask", "Setting som variables");
        try {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(
                    getString(R.string.URL));
            Log.d("GetStudentsByTeamTask", "Instance the HttpTransportSE");

            // this is the actual part that will call the webservice
            androidHttpTransport.call(
                    getString(R.string.Soap_Action_GetStudentsByTeam),
                    envelope);
            Log.d("GetStudentsByTeamTask", "Called the Webservice");

            // Get the SoapResult from the envelope body.
            SoapObject result = (SoapObject) envelope.getResponse();
            Log.d("GetStudentsByTeamTask", "Got the Soapresult");

            if (result != null) {
                // Do something with result
                // success = true;
                Log.d("GetStudentsByTeamTask", "set sucess boolean to true");

                for (int i = 0; i < result.getPropertyCount(); i++) {

                    PropertyInfo pi = new PropertyInfo();
                    result.getPropertyInfo(i, pi);
                    Log.d("GetStudentsByTeamTask",
                            pi.name + " : " + result.getProperty(i));

                    SoapObject obj = (SoapObject) result.getProperty(i);

                    Student student = new Student();

                    student.address = obj.getProperty("Address").toString();

                    student.city = obj.getProperty("City").toString();
                    student.created = DateTime.parse(obj.getProperty(
                            "Created").toString());
                    student.dateOfBirth = DateTime.parse(obj.getProperty(
                            "DateOfBirth").toString());
                    student.email = obj.getProperty("Email").toString();
                    student.firstname = obj.getProperty("FirstName")
                            .toString();
                    student.id = Integer.parseInt(obj.getProperty("ID")
                            .toString());
                    student.imageId = Integer.parseInt(obj.getProperty(
                            "ImageID").toString());
                    // SoapObject lastNameObject = (SoapObject) obj
                    // .getProperty("LastName");
                    //
                    student.lastName = obj.getProperty("LastName")
                            .toString();

                    student.phone = obj.getProperty("Mobile").toString();

                    student.zipcode = obj.getProperty("PostalCode")
                            .toString();
                    student.schoolId = Integer.parseInt(obj
                            .getPropertyAsString("SchoolId"));
                    student.teamId = Integer.parseInt(obj
                            .getPropertyAsString("TeamId"));
                    student.testStarted = Integer.parseInt(obj
                            .getPropertyAsString("TestsStarted"));
                    student.timeStamp = DateTime.parse(obj
                            .getPropertyAsString("TimeStamp"));
                    student.image = getImage(Integer.parseInt(obj
                            .getProperty("ImageID").toString()));

                    if (student.image == null)
                        student.image = BitmapFactory
                                .decodeResource(getResources(),
                                        R.drawable.default_usericon);

                    MyApp.getController().addStudent(student);
                }

            } else {
                // If fails
                // success = false;
                Log.d("GetStudentsByTeamTask", "set login boolean to false");
            }
        } catch (Exception e) {
            Log.d("GetStudentsByTeamTask", "FAILED! " + e.getMessage());
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2012-08-31
    • 2011-11-21
    • 2012-10-01
    • 1970-01-01
    相关资源
    最近更新 更多