改造2重复请求

时间:2016-06-25 23:40:34

标签: android http-post retrofit2

有人能帮帮我吗?我使用改造2用post方法发送数据,但是我发送了3次。

这是我的服务生成器:

public class ServiceGenerators {
    private static final String TAG = ServiceGenerators.class.getSimpleName();
    //private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();


    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(Constant.API_URL)
            .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        //loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);/
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
                Response response = chain.proceed(original);

                Log.e(TAG, original.url().toString());

                Request.Builder requestBuilder = original.newBuilder()
                        .header("Access-key", "0c3ed6a45e33e047c4e3785cb87aa6b27e651c52")
                        .method(original.method(), original.body());


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


            }
        });
        OkHttpClient client = httpClient.build();
        Retrofit retrofit = builder.client(client).build();
        return retrofit.create(serviceClass);
    }
}

Kejadian服务

@FormUrlEncoded
    @POST("/api/kejadian")
    Call<Responses> postKejadian(
            @Field("kejadian") String kejadian,
            @Field("w_kejadian") String w_kejadian,
            @Field("lokasi") String lokasi,
            @Field("keterangan") String keterangan,
            @Field("id_user") int id_user
    );

这是我的过程:

KejadianService service = ServiceGenerators.createService(KejadianService.class);
        Call<Responses> call = service.postKejadian(kejadian.getText().toString(), tanggal, lokasi.getText().toString(), keterangan.getText().toString(), session.getUID());


    call.enqueue(new Callback<Responses>() {
        @Override
        public void onResponse(Call<Responses> call, Response<Responses> response) {
            pd.dismiss();
            int code = response.code();
            if (code == 200){
                Responses responses = response.body();
                if (responses.error == null) {
                    Constant.showDialog(getApplicationContext(), "Kejadian berhasil dikirim!", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
                } else {
                    Snackbar.make(findViewById(R.id.drawer_layout), responses.error, Snackbar.LENGTH_LONG).show();
                }
                call.cancel();
            }else if (code == 500){
                Snackbar.make(findViewById(R.id.drawer_layout), "Error in server", Snackbar.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<Responses> call, Throwable t) {

        }
    });

我的日志

  

06-26 06:27:39.640 17813-18294 / E / ServiceGenerators:http // myurl / api / kejadian   06-26 06:27:39.640 17813-18294 I / System:core_booster,getBoosterConfig = false   06-26 06:27:40.070 17813-18294 E / ServiceGenerators:http // myurl / api / kejadian   06-26 06:27:40.070 17813-18294 I / System:core_booster,getBoosterConfig = false   06-26 06:27:40.520 17813-18294 E / ServiceGenerators:http // myurl / api / kejadian   06-26 06:27:40.520 17813-18294 I / System:core_booster,getBoosterConfig = false

我希望你明白我的意思,谢谢

1 个答案:

答案 0 :(得分:0)

删除此行代码

Response response = chain.proceed(original);

chain.proceed()执行请求并返回响应。 有关详细说明,请参阅 https://github.com/square/okhttp/wiki/Interceptors