【问题标题】:Consuming WCF Service with Retrofit通过改造使用 WCF 服务
【发布时间】:2018-08-07 08:39:54
【问题描述】:

我有这种情况。 我有一个用 C# 制作的 WCF Web 服务,我需要在 Android 上使用 Retrofit 来使用它。我搜索了很多,找到了一些帖子和网站,但我仍然有问题。

这是我的网络服务文件(其中之一)

[OperationContract]
[WebInvoke(
    Method = "GET", 
    UriTemplate = @"/Login?UserName={userName}&Password={password}", 
    ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Wrapped)
]
string Login(string userName, string password);

我的 Web 服务运行良好,当我从浏览器调用它时,我得到以下响应:

{"UserList":[{"UserName":"Juan Perez","Active":1,"ProfileName":"Admin"}]}

在我的 Android 应用上,我有这些文件:

User.java

public class User {
    @SerializedName("UserName")
    @Expose
    private String userName;
    @SerializedName("Active")
    @Expose
    private String active;
    @SerializedName("ProfileName")
    @Expose
    private String profileName;

    public User(String userName, String active, String profileName) {
        this.userName = userName;
        this.active =  active;
        this.profileName = profileName;
    }

    public String getUserName() {
        return userName;
    }
    public String getActive() {
        return active;
    }
    public String getProfileName() {
        return profileName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public void setActive(String active) {
        this.active = active;
    }
    public void setProfileName(String profileName) {
        this.profileName = profileName;
    }
}

UserList.java

public class UserList {
    @SerializedName("UserList")
    private ArrayList<User> userList;

    public ArrayList<User> getUserList() {
        return userList;
    }

    public void setUserList(ArrayList<User> userList) {
        this.userList = userList;
    }
}

我有这个抽象类用于改造

public abstract class IWebservice {
    private static final String BASE_URL = "http://mypc.domain.com/Android.svc/";
    public Retrofit retrofit;

    public Retrofit getRetrofit() {
        if(this.retrofit == null) {
            this.retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }
}

我还有这个文件

public interface ILoginService {
    @GET("Login")
    Call<UserList> Login(@Query("UserName") String userName, @Query("Password") String password);
}

在这里我使用网络服务

public class Login extends IWebservice {
    public void isValidUser(String userName, String password) {
        ILoginService loginService = getRetrofit().create(ILoginService.class);

        Call<UserList> userData = loginService.Login(userName, password);

        userData.enqueue(new Callback<UserList>() {
            @Override
            public void onResponse(Call<UserList> call, Response<UserList> response) {
                UserList usersInformation = response.body();

                if(usersInformation.getUserList().size() > 0) {
                    EventBus.getDefault().post(new Error("Usuario valido" + usersInformation.getUserList().get(0).getUserName()));
                }
                else {
                    EventBus.getDefault().post(new Error("Usuario Invalido"));
                }
            }

            @Override
            public void onFailure(Call<UserList> call, Throwable t) {
                EventBus.getDefault().post(new Error("Error: " + t.getMessage()));
            }
        });
    }
}

最后在我的 MainActivity.java 上我这样称呼它:

private void doLogin(String userName, String password) {
    Login login = new Login();
    login.isValidUser(userName, password);
}

现在,问题是我总是得到 200(OK)的状态,但是 Login.java 上的 response.body() 总是为空,我不知道为什么,任何线索伙计们?

编辑 我添加了 HttpLoggingInterceptor 并得到了这个。

D/OkHttp: <-- 200 OK http://mypc.domain.com/Android.svc/Login?UserName=JUANP&Password=123456 (102ms)
D/OkHttp: Cache-Control: private
D/OkHttp: Content-Length: 115
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Server: Microsoft-IIS/7.5
D/OkHttp: X-AspNet-Version: 4.0.30319
D/OkHttp: X-Powered-By: ASP.NET
D/OkHttp: Date: Sat, 04 Aug 2018 22:18:28 GMT
D/OkHttp: {"LoginResult":"{\"UserList\":[{\"UserName\":\"Juan Perez\",\"Active\":1,\"ProfileName\":\"Admin\"}]}"}
D/OkHttp: <-- END HTTP (115-byte body)

我的结果被包裹在 LoginResult 中,知道为什么吗?我想这就是我遇到问题的原因。

非常感谢您的任何帮助:)

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    你应该添加另一个类

    public class LoginResult {
        @SerializedName("LoginResult")
        @Expose
        private UserList userList;
    }
    

    并将您的界面更改为

    public interface ILoginService {
    @GET("Login")
    Call< LoginResult > Login(@Query("UserName") String userName, @Query("Password") String password);
    

    【讨论】:

    • 感谢您的回答...我不得不更改我的 Web 服务上的一些代码,我注意到我返回的是字符串,而不是对象(使用 Newtonsoft 序列化为 Json),但这很方便我在另一项服务上返回的真实列表。 .再次,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2011-07-08
    • 1970-01-01
    • 2010-10-31
    • 2011-11-21
    • 2010-11-19
    相关资源
    最近更新 更多