当前位置: 首页 > 知识库问答 >
问题:

通过Rest web服务从android登录,使用改版不工作

相俊迈
2023-03-14
http://test:q1w2e3r4@192.168.0.110:8080/liferay-portlet/api/secure/jsonws/
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

如果我把它放入邮递员中,它就会正确地获取json响应。

现在,当我从我的android代码中调用相同的代码时,我得到的响应是“unauthorized”。

我的改装服务

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类(该类用于调用服务接口并创建翻新构建器)

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

共有1个答案

邵伟泽
2023-03-14

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

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);
 类似资料:
  • 首先,我想让你知道,我是新来的Symfony。我在路上把我的PHP项目从我自己的“基本”MVC转到SimfOn.该项目已经运行良好,但我在适应Symfony时遇到了一些问题。 我从基本的框架开始,make: user和make: auth。模板运行良好。但是我未能将登录过程转换为AJAX和JSON。 我遵循了这个官方教程:https://symfonycasts.com/screencast/ap

  • 我有一个奇怪的问题与谷歌登录和谷歌播放服务。我按照这里的说明https://developers.google.com/identity/sign-in/android/start-integrating添加了到我的build.gradle文件。 然后,我在应用程序的gradle文件中添加了应用程序所需的这些播放服务库: 但是当我编译时,我得到了这个错误: > 出了什么问题:任务执行失败:应用程序

  • 创建restful应用程序,但它不会以XML格式返回响应。即使在点击URL时控制台上也没有日志“http://localhost:8080/message/webapi/messages". 我返回一个列表,并使用@products(MediaType.APPLICATION\uxml)以XML形式返回响应。 消息资源。Java语言 Message.java 如果我不使用@XMLRootEleme

  • 我尝试使用spring security进行登录,但它不起作用,因为它返回给我这个链接:http://localhost:8080/login?error并在主页上报告我:这是代码: 静态页面中的一个索引,它重新链接我: 在模板中,我有以下内容: 并且捕获它的请求映射就在那里: 日志:在6.958秒内启动了ProgettoSiwApplication(JVM运行7.964)2018-06-21 1

  • 嗨,我正在尝试对我的登录页面进行Microsoft单点登录身份验证。为此,我已经使用它创建了Azure多租户目录,并根据给定的链接https://www.c-sharpconer.com/article/azure-ad-authentication-for-mvc-web-application/创建了带有重定向URI的应用程序。成功地集成了代码,当运行时,我在输入用户名和密码后,我在错误下面得

  • 我正在使用laravel 5.4开发一个api 尝试使用API控制器登录时 它给我以下错误: {FatalThrowableError调用未定义的方法Illumate\Auth\TokenGuard::attempt()}