2017-10-30 3 views
-1

Retrofit funktioniert, aber nicht auf Erfolg und auf Fehlermethode beim Debuggen aufrufen.Nachrüstung funktioniert, ruft aber keinen Erfolg auf und bei Fehlermethode

Dies ist der Code

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(); 

     } 

    } 

Methode:

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 Client-Klasse:

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 Schnittstelle:

//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); 
+0

mit Wie man sagen kann 'onSuccess' nicht genannt zu werden, und es wird bei der Arbeit gleiche Zeit? –

+0

es funktioniert, aber auf Debug-Antwort ist null –

+0

Was ist Ihr genaues Problem plzz aufwändig –

Antwort

1

Sie sollten überprüfen, ob die Anforderung erfolgreich ist oder nicht von isSuccessful innerhalb onResponse

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