通过改造使用WCF服务

时间:2018-08-04 21:56:29

标签: android retrofit

我有这种情况。 我有一个使用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);
}

在这里我使用Web服务

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(确定)的状态,但是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 中,知道为什么吗?我认为这就是为什么我遇到问题。

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您应该将另一个类添加为

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