从改造2 android发布关于django api的数据

时间:2018-03-27 05:30:40

标签: android django-rest-framework retrofit2

我正在开发一个项目,它将通过django api检索并向服务器发送数据。我正面临着通过api POST数据的问题。我的代码如下。在android studio中执行此代码会给出401 Error的响应,但api在postman或web浏览器中工作正常。 TIA的帮助。

设置改造实例的类:

public class ApiClient {
   public static final String BASE_URL = "https://myapilink.com/";

private static Retrofit retrofit = null;

public static Retrofit getClient() {

    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            // Request customization: add request headers
            Request.Builder requestBuilder = original.newBuilder()
                    .header("Authorization", "auth-value"); // <-- this is the important line

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


    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();

                    // Request customization: add request headers
                    Request.Builder requestBuilder = original.newBuilder()
                            .header("Authorization", "Basic YWRtaW46MTIzNA=="); // <-- this is the important line

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

    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .client(okHttpClient)
                .build();
    }
    return retrofit;
}

改造界面:

public interface ApiInterface {

@Headers("Content-Type: application/json")
@POST("/api/auth/login/")
Call<LoginModel> getLoginResponseWithRetrofit(@Body LoginBody loginBody);
}

LoginBody模型:

public class LoginBody {

String email;
String password;

public LoginBody(String email, String password){
    this.email=email;
    this.password=password;
 }

}

主要api调用方法:

public void postDataWithRetrofit(String email, String password){

    //modelFromIDProvider.add(InternalDataProvider.getInstance().getAgentDataModelList());


    int selectedPosition,id;

    LoginBody loginBody = new LoginBody(email, password);

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

    Call<LoginModel> call;

    Map<String, String> formData = new HashMap<>();
    formData.put("email", email);
    formData.put("password", password);

    Log.d(TAG,"formdata "+formData.toString());

    //call = apiService.getLoginResponseWithRetrofit(email, password);

    call = apiService.getLoginResponseWithRetrofit(loginBody);

    call.enqueue(new Callback<LoginModel>() {

        @Override
        public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
            Log.d(TAG,"isSuccessful "+response.isSuccessful()+" code "+response.code());

            Log.d(TAG,"Data from response : "+response.body());
            if(!response.isSuccessful()) {
                dialog.dismiss();
                return;
            }

            LoginModel responseModel = response.body();
            InternalDataProvider.getInstance().setLoginDataModel(responseModel);

            Log.d(TAG,"Data from response : "+response.body());
        }

        @Override
        public void onFailure(Call<LoginModel> call, Throwable t) {
            if(call.isCanceled()){
                Log.d(TAG," call cancelled "+t.toString());
                return;
            }

            Log.d(TAG," inside onFailure "+t.toString());
        }
    });
}

0 个答案:

没有答案