发送POST方法时未经授权

时间:2019-05-23 08:43:53

标签: android spring retrofit

我对翻新和后期处理方法有疑问。 我的GET方法具有身份验证功能,但是POST无法正常工作。 我登录时没有任何问题,并且执行任何GET方法。

当我尝试向REST API发送一些数据(春季)时,出现此错误:

  

Response {protocol = http / 1.1,code = 401,message =,url = {http://10.101.12.31:8080/add}

我的代码:

DataAPI.java:

public class DataAPI {
    private static final String BASE_URL = "http://10.101.12.31:8080/";
    static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());
    private static Retrofit retrofit = builder.build();

    public static <S> S createService(Class<S> serviceClass) {
        return createService(serviceClass, null, null);
    }

    public static <S> S createService(
            Class<S> serviceClass, String username, String password) {
        if (!TextUtils.isEmpty(username)
                && !TextUtils.isEmpty(password)) {
            String authToken = Credentials.basic(username, password);
            return createService(serviceClass, authToken);
        }

        return createService(serviceClass, null);
    }

    public static <S> S createService(
            Class<S> serviceClass, final String authToken) {
        if (!TextUtils.isEmpty(authToken)) {
            AuthenticationInterceptor interceptor =
                    new AuthenticationInterceptor(authToken);

            if (!httpClient.interceptors().contains(interceptor)) {
                httpClient.addInterceptor(interceptor);

                builder.client(httpClient.build());
                retrofit = builder.build();
            }
        }

        return retrofit.create(serviceClass);
    }



    public static DepartmentService getDepartmentService() {
        return retrofit.create(DepartmentService.class);
    }
}

AuthenticationInterceptor.java:

public class AuthenticationInterceptor implements Interceptor {

    private String authToken;

    public AuthenticationInterceptor(String token) {
        this.authToken = token;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request.Builder builder = original.newBuilder()
                .addHeader("Authorization", authToken);

        Request request = builder.build();
        return chain.proceed(request);
    }
}

Web界面:

public interface Service {
    @POST("/add") 
    Call<Void> addUser(@Body User user);
}

方法:

public void buttonOne(View v) {
        DataAPI.createService(Service.class, getIntent().getStringExtra("authToken"))
                .addUser(user)).enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                System.out.println(response);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(getApplicationContext(),t.toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }

0 个答案:

没有答案