改造有效但不会要求成功和失败方法

时间:2017-10-30 12:19:22

标签: android retrofit

Retrofit有效,但在调试时不会调用成功和失败方法。

这是代码

  database = new FishDb(this);
    data = database.getTournaments();

    if (data.size() > 0) {

        for (int i = 0; i < data.size(); i++) {
            user_id = data.get(i).getUser_id();
            name = data.get(i).getTournament_name();
            address = data.get(i).getTournament_location();
            date = data.get(i).getTournament_date();
            ftournament_id = data.get(i).getTournament_id();
            tournament_name = data.get(i).getTournament_name();

            start();

        }

    }

方式:

private void start() {
    ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<ResponseBody> call = apiInterface.startTournament(user_id, name, address, date, latitude, longitude);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

            String jsonStr = "";

            try {
                jsonStr = response.body().string();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Log.e("StartTournament", jsonStr);
            try {
                json = new JSONObject(jsonStr);

                if (json.toString().contains("status")) {

                    jstr = json.getString("status");

                    if (jstr.equals("success")) {

                        if (json != null) {
                            tournament_id = json.getString("tournament_id");

                            sessionManager.tournament(tournament_id, latitude, longitude, name);

                            fish_data = database.getFish(ftournament_id);

                            if (fish_data.size() > 0) {

                                for (int j = 0; j < fish_data.size(); j++) {

                                    fish_id = fish_data.get(j).getId();
                                    selectedFilePath = fish_data.get(j).getImage();
                                    fish_color = fish_data.get(j).getColor();
                                    fish_tag = fish_data.get(j).getTag();
                                    fish_type = fish_data.get(j).getType();
                                    fish_weight = fish_data.get(j).getWeight();
                                    fish_length = fish_data.get(j).getLength();
                                    fish_lengthType = fish_data.get(j).getLength_type();
                                    fish_weightType = fish_data.get(j).getWeight_type();
                                    date_time = fish_data.get(j).getDate();

                                    addFish();

                                }

                            }
                            database.deleteAllFish();

                        }


                    } else if (jstr.equals("failure")) {

                    }

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

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

        }
    });
}

Api客户端类:

public static final String BASE_URL = "http://ds.com/f_Api/";
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

Api界面:

 //Start Tournament
    @FormUrlEncoded
    @POST("tournament.php?action=start")
    Call<ResponseBody> startTournament(@Field("user_id")String user_id,
                                       @Field("name") String name,
                                       @Field("location")String location,
                                       @Field("time")String date,
                                       @Field("lat")String lat,
                                       @Field("lon")String lon);

1 个答案:

答案 0 :(得分:1)

您应该使用isSuccessful内的onResponse来检查请求是否成功

if(response.isSuccessful()){
    String data = response.body().string();
} else {
    //response.body() will return null.
    //use response.errorBody();
    String errorMesaage = response.errorBody().string();
}