使用RETROFIT进行make post请求时出错

时间:2018-03-16 12:29:49

标签: android

我在使用改造方面遇到了问题。我有一个API https://raakar.ir/addProject,想要发送一些信息。 API在邮递员中工作正常。我想发帖请求。我想,我认为每一个都是正确的。但是当我运行应用程序时,它会崩溃。 我使用了这些库:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

这些是我改造的界面:

public interface APIInterface {

@Multipart
@POST("addProject")
Call<AddProjectResponse> post(
        @Header("token") String token,
        @Field("name") String name,
        @Field("amount") String amount,
        @Field("description") String description,
        @Field("category") String category,
        @Field("deadline") String deadline,
        @Field("projectFile")Bitmap bitmap
        );
}

-------------------------------------------- -------------------------------------------------- --------------------------------------

Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl("https://raakar.ir/")
                    .addConverterFactory(GsonConverterFactory.create());
    Retrofit retrofit = builder.build();

    APIinterface retroInterface = retrofit.create(APIinterface.class);

    Call<AddProjectResponse> call = retroInterface.post(token,
            "طراحی راکار",
            "50000000",
            "خالی است",
            "برنامه نویسی",
            "5",
            null);

    call.enqueue(new Callback<AddProjectResponse>() {
        @Override
        public void onResponse(Call<AddProjectResponse> call, Response<AddProjectResponse> response) {
            Log.d("Resposne:", response.toString());
        }

        @Override
        public void onFailure(Call<AddProjectResponse> call, Throwable t) {
            Log.d("Respone:", "Error");
        }
    });

帮助我!

2 个答案:

答案 0 :(得分:0)

我想,如果你没有发布任何图像和其他文件,如视频等,那么你不能使用@Multipart并传入post方法paremater为此制作一个pojo类,只传递你的对象,如下面...样本代码

       MyEventRequestModel myEventListRequestModel = new MyEventRequestModel(); // define your pojo class object 
    myEventListRequestModel.setDate(mDate);
    Call<MyEventResponseModel> call = apiInterface.getAllEvent(myEventListRequestModel);

和标题将在改装初始化时设置..

OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (context == null) {
                request = request
                        .newBuilder()
                        .build();
            } else {
                request = request
                        .newBuilder()
                        .addHeader("Authorization", "Bearer " + AppSetting.getStringSharedPref(context, Constants.USER_KEY_TOKEN, "")) // hear define your header.
                        .build();
            }
            return chain.proceed(request);
        }
    });

答案 1 :(得分:0)

如果您使用@Multipart而不是使用@Part而不是@Field

  public interface APIInterface {
  @Multipart
  @POST("addProject")
      Call<AddProjectResponse> post(
      @Part ("token") String token,
      @Part ("name") String name,
      @Part ("amount") String amount,
      @Part ("description") String description,
      @Part ("category") String category,
      @Part ("deadline") String deadline,
      @Part MultipartBody.Part img
    );
}