gpt4 book ai didi

android - 使用改造无法通过 Android 的 Rest Web 服务登录

转载 作者:可可西里 更新时间:2023-11-01 19:06:45 24 4
gpt4 key购买 nike

我正在使用 Retrofit 2 制作一个安卓应用。我的 REST Api 都是用 Liferay 写的。现在在 Liferay 中,我所看到的是,要访问我们需要首先进行身份验证的 Web 服务。所以我已经这样验证了

http://test:q1w2e3r4@192.168.0.110:8080/liferay-portlet/api/secure/jsonws/

Liferay 有自己的用户身份验证方法,我们已经覆盖了它。我检查了来自 Postman 的 Web 服务调用,它工作正常。

URL:http://test:q1w2e3r4@192.168.0.110:8080/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name

表单编码值

companyId:10154
screenName:xyz
password:xyz
active:true

如果我把它放在 postman 中,它会正确获取 json 响应。

现在,当我从我的 android 代码中调用相同的代码时,我会收到“未经授权”的响应。

我的 Retrofit 服务

public interface LoginApi {    
@FormUrlEncoded
@POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name")
Call<User> login(@Field("companyId")long companyId,@Field("screenName")String screenName,@Field("password")String password,@Field("active")boolean active);
}

我的RestApiManager类(该类用于调用服务接口(interface)和创建retrofit builder)

public class RestApiManager {

private LoginApi loginApi;

public LoginApi login() {
if (loginApi==null) {
GsonBuilder gson=new GsonBuilder();
gson.registerTypeAdapter(String.class, new StringDeserializer());
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://test:q1w2e3r4@192.168.0.110:8080")
.addConverterFactory(GsonConverterFactory.create())
.build();
loginApi=retrofit.create(LoginApi.class);
}
return loginApi;
}

调用 RestApiManager

Call<User> callUser=restApiManager.login().login(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive());
callUser.enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
Log.d("Login","Login Response:"+response.body());
}

@Override
public void onFailure(Throwable t) {
Log.d("Login","Login Response:"+t.getMessage());
}
});

最佳答案

看起来您的请求应该有一个 JSON 主体而不是 POST 变量?您正在调用 JSON 网络服务,您的示例参数看起来比 POST 更像 JSON。如果是这样,那么您可以将您的参数封装在一个对象中——

public class User {
int companyId;
String screenName;
String password;
boolean active;

User(int companyId, String screenName, String password, boolean active) {
this.companyId = companyId;
this.screenName = screenName;
this.password = password;
this.active = active;
}

你的界面会是——

public interface LoginApi {    
@POST("/liferay-portlet/api/secure/jsonws/customuserauthentication/authenticate-by-user-name")
Call<User> login(@Body User user);
}

并将你的调用构造为 --

User user = new User(loginData.getCompanyId(),loginData.getScreenName(),loginData.getPassword(),loginData.isActive());
Call<User> callUser = restApiManager.login().login(user);

关于android - 使用改造无法通过 Android 的 Rest Web 服务登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33524474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com