4

私は、wcf サービスを使用する Android で最初のアプリケーションを構築しています。応答を解析するために ksoap2 を使用しています。応答は、実際には C# で定義されたオブジェクトの配列です。この非常に役立つガイドに従ってこれを行いました。今、私の問題はC# でオブジェクトの配列を返す wcf サービスを使用する必要がありますが、今回はこれらのオブジェクトの一部のプロパティが他のオブジェクトです。私の質問は、内部オブジェクトをマップしてプロパティを解析できるようにするにはどうすればよいですか?

よくわからない場合のために、次のようなオブジェクトを解析する必要があります。

public class OutterObject {

     private InnerObject1 io1;
     private InnerObject2 io2;

}

私が十分に明確だったことを願っています

わかりました、これは私の簡略化されたコードです。すべてを投稿することはできません。問題があると思われる部分だけです。

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    Request.addProperty("connString",args.get(0));
    Request.addProperty("ClCode", args.get(1));
    Request.addProperty("TeCode", args.get(2));
    Request.addProperty("CourseDate", args.get(3));               



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


    envelope.addMapping(namespace, "SRV_WeekProgramm",newSRV_WeekProgramm().getClass());
    envelope.addMapping(namespace, "Course", new Course().getClass());
    envelope.addMapping(namespace, "StudentHours", new StudentHours().getClass());

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


    androidHttpTransport.call(SOAP_ACTION, envelope);    


    Course response = (Course)envelope.bodyIn; //this is where it crashes

これはスローされた例外です: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to connectionInitializer.Course

4

1 に答える 1

3

これが私のために働いた例です。
これは、Webサービスからの応答メッセージタイプです

  <message name="loginUserResponse">
     <part name="code" type="xsd:int"/>
     <part name="desc" type="xsd:string"/>
     <part name="user" type="tns:user"/>
  </message>

メソッドloginUser定義

<operation name="loginUser">
   <documentation>Login user.</documentation>
   <input message="tns:loginUserRequest"/>
   <output message="tns:loginUserResponse"/>
</operation>

クラスUserResponse(Outter)にはUser次のプロパティが含まれています。

public class UserResponse  implements KvmSerializable {
public int code;
public String desc;
public User user;
public Object getProperty(int arg0) {
    switch (arg0) {
    case 0:
        return code;
    case 1:
        return desc;
    case 2: 
        return user;
    default:
        break;
    }
    return null;
}

public int getPropertyCount() {
    return 3;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch (index) {
    case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "code";
        break;
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "desc";
        break;
    case 2:
        info.type = User.class;
        info.name = "user";
        break;
    default:
        break;
    }

}

public void setProperty(int index, Object value) {
    switch (index) {
    case 0:
        this.code = Integer.parseInt(value.toString());
        break;
    case 1:
        this.desc = value.toString();
        break;
    case 2:
        this.user = (User) value;
    default:
        break;
    }       
}
  }

そしてUserクラス(インナー)

 public class User implements KvmSerializable  {
public int user_id;
public String username;
public String email;
public String password;

public User() {

}

public Object getProperty(int index) {
    switch (index) {
    case 0:
        return user_id;
    case 1:
        return username;
    case 2:
        return email;
    case 3:
        return password;
    default:
        return null;
    }
}

public int getPropertyCount() {
    return 4;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch (index) {
    case 0:
        info.type = PropertyInfo.INTEGER_CLASS;
        info.name = "user_id";
        break;
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "username";
        break;
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "email";
        break;
    case 3:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "password";
        break;
    default:
        break;
    }
}

public void setProperty(int index, Object value) {
    if(null == value)
        value = "";
    switch (index) {
    case 0:
        user_id = Integer.parseInt(value.toString());
        break;
    case 1:
        username = value.toString();
        break;
    case 2:
        email = value.toString();
        break;
    case 3:
        password = value.toString();
        break;
}
   }

これは重要です。必ず外部クラスと内部クラスの両方のキーを登録してください。

  envelope.addMapping(NAMESPACE, "loginUserResponse", UserResponse.class);
  envelope.addMapping(NAMESPACE, "user", User.class);

最後に、キャストすることで結果を得ることができます。

   HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL); //open the requisition
   androidHttpTransport.call(SOAP_ACTION, envelope);// call
   UserResponse response = (UserResponse)envelope.bodyIn;

この助けを願っています!

于 2012-10-16T10:13:25.240 に答える