首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改造2总是返回onFailure

改造2总是返回onFailure
EN

Stack Overflow用户
提问于 2017-05-13 09:01:06
回答 1查看 2.7K关注 0票数 0

我不明白这个问题。总是返回onFailure。数据返回状态代码200,OnResponse不返回。

响应D/OkHttp:<- 200 OK http://192.168.10.10/oauth/token (667 OK)

这个密码怎么了?

App.java

代码语言:javascript
复制
public class App extends Application {

    private static ApiBackend api;
    private Retrofit retrofit;

    @Override
    public void onCreate() {
        super.onCreate();

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();


        retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.10.10/")
                .addConverterFactory(GsonConverterFactory.create())
               .client(client)
                .build();
        api = retrofit.create(ApiBackend.class);
    }

    public static ApiBackend getApi() {
        return api;
    }
}

接口

代码语言:javascript
复制
@FormUrlEncoded
@POST("oauth/token")
Call<List<AuthModel>> auth(
        @Field("username") String username,
        @Field("password") String password,
        @Field("grant_type") String grant_type,
        @Field("client_id") Integer client_id,
        @Field(

"client_secret") String client_secret
            );

AuthModel

代码语言:javascript
复制
public class AuthModel {
    List<AuthModel> TaskModel;
    @SerializedName("username")
    @Expose
    private String username;
    @SerializedName("password")
    @Expose
    private String password;
    @SerializedName("grant_type")
    @Expose
    private String grantType;
    @SerializedName("client_id")
    @Expose
    private Integer clientId;
    @SerializedName("client_secret")
    @Expose
    private String clientSecret;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }

    public Integer getClientId() {
        return clientId;
    }

    public void setClientId(Integer clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

}

MainActivity

代码语言:javascript
复制
        App.getApi().auth("admin@admin.com","admin123","password",2,"yl9s3UK74vSR1AxlCMhHqHO1AbTz9DzaXUVZyKpA").enqueue(new Callback<List<AuthModel>>() {
        @Override
        public void onResponse(Call<List<AuthModel>> call, Response<List<AuthModel>> response) {
            if (response.isSuccessful()) {
              Log.v(TAG,"ffff");
            } else {
                // error response, no access to resource?
            }
        }

        @Override
        public void onFailure(Call<List<AuthModel>> call, Throwable t) {
            Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
        }
    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-13 10:52:10

问题是,您无法理解如何使用Retrofit库。

让我们看看您在服务接口中声明的方法。

代码语言:javascript
复制
@FormUrlEncoded
@POST("oauth/token")
Call<List<AuthModel>> auth(
    @Field("username") String username,
    @Field("password") String password,
    @Field("grant_type") String grant_type,
    @Field("client_id") Integer client_id,
    @Field(

"client_secret") String client_secret
        );

Call<List<AuthModel>>行告诉修改api调用的响应将是什么样子。您在这里告诉它要做的是期待一些类似于您的AuthModel的东西,不仅是这样,而且还有一个AuthModels列表。

你真正需要做的是

  1. 创建一个类,它表示响应{ "token_type": "Bearer", "expires_in": 31536000}
  2. 声明为您的auth方法调用的预期响应,类似于so Call<MyFunkyNewLoginResponseClass>

如果你在那之后还在经历问题,那就应该有一个单独的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43951222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档