2017-12-16 11 views
0

まずこの質問は何度も尋ねられています。しかし、私は問題の解決策を見つけることができません。ログから、WebサービスがJSONを正しく返すことがわかります。しかし何らかの理由でそれはonResponseメソッドに入ることはありません。誰かが私にこのことについてのヒントを与えることができれば、とても感謝しています。更新2のonResponseメソッドが呼び出されることはありません

public void getAllTipo_evento() { 
     mTipo_eventoService.getAll().enqueue(new Callback<List<Tipo_eventoDTO>>() { 
      @Override 
      public void onResponse(Call<List<Tipo_eventoDTO>> call, Response<List<Tipo_eventoDTO>> response) { 

       if(response.isSuccessful()) { 
        Log.d("MainActivity", "posts loaded from API"); 
       }else if(response.errorBody() != null){ 
         try { 
          String errorBody = response.errorBody().string(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 
        } 
       else { 
        int statusCode = response.code(); 
        // handle request errors depending on status code 
       } 
      } 

      @Override 
      public void onFailure(Call<List<Tipo_eventoDTO>> call, Throwable t) { 
       t.printStackTrace(); 
       Log.d("MainActivity", "error loading from API"); 

      } 
     }); 
    } 

RetrofitClient

public class RetrofitClient { 

    private static Retrofit retrofit = null; 

    public static Retrofit getClient(String baseUrl){ 

     Log.d("RetrofitClient.LOGTAG", "HTTPClient"); 

     HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
     interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
     OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 

     if (retrofit == null){ 
      retrofit = new Retrofit.Builder() 
        .baseUrl(baseUrl) 
        .client(client) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 
    } 
} 

インタフェース

public interface Tipo_eventoService { 
    @GET("tipo_evento/getAll") 
    Call<List<Tipo_eventoDTO>> getAll(); 

} 

ログ出力

D/RetrofitClient.LOGTAG: HTTPClient 
D/RetrofitClient.LOGTAG: HTTPClient 
D/OkHttp: --> GET http://10.0.2.2:3000/tipo_evento/getAll 
D/OkHttp: --> END GET 
D/OkHttp: <-- 200 OK http://10.0.2.2:3000/tipo_evento/getAll (63ms) 
D/OkHttp: Content-Type: application/json; charset=utf-8 
D/OkHttp: Date: Sat, 16 Dec 2017 00:32:33 GMT 
D/OkHttp: Content-Length: 286 
D/OkHttp: [{"ID":1,"Nombre":"Shopping"},{"ID":2,"Nombre":"Night Life"},{"ID":3,"Nombre":"Fun and games"},{"ID":4,"Nombre":"Classes and workshops"},{"ID":5,"Nombre":"Food and beverage"},{"ID":6,"Nombre":"Concert and show"},{"ID":7,"Nombre":"Outdoor activity"},{"ID":8,"Nombre":"Wellness centres"}] 
D/OkHttp: <-- END HTTP (286-byte body) 

TipoEventoDTO.java

public class Tipo_eventoDTO { 
    @SerializedName("Id") 
    @Expose 
    private int Id; 
    @SerializedName("nombre") 
    @Expose 
    private String nombre; 

    public Tipo_eventoDTO(int id, String nombre) { 
     this.Id = id; 
     this.nombre = nombre; 
    } 

    public int getId() { 
     return Id; 
    } 

    public void setId(int id) { 
     this.Id = id; 
    } 

    public String getNombre() { 
     return nombre; 
    } 

    public void setNombre(String nombre) { 
     this.nombre = nombre; 
    } 
} 
+0

を働くなら、私が知っているようだと思います:1、 "Nombre": "Shopping"}、{"ID":2、 "Nombre": "Night Life"}、{"ID":3、 "Nombre": "Fun and games"}、{"ID "、" Nombre ":"クラスとワークショップ "}、{" ID ":5、" Nombre ":"飲食 "}、{ID:6、" Nombre ":"コンサートとショー "} {"ID":7、 "Nombre": "屋外活動"}、{"ID":8、 "Nombre": "ウェルネスセンター"}] 'OKです。 – KeLiuyue

+0

クラスオブジェクトが一致しないときに 'Tipo_eventoDTO'が呼び出されない場合があります –

+0

私は解析しているprobがあると思います。 'enqueue()'の代わりに 'execute()'を呼び出して出力を確認できますか? – Henry

答えて

1

私はこの問題は、あなたのPOJO @SerializedNameの親切

@SerializedName("Nombre")

にご@SerializedName("Id")@SerializedName("ID")@SerializedName("nombre")に変更することはあなたは `[{ "ID" をログ

+0

それはまさにその、 どうもありがとうございました! – user2912069

+0

問題ありません:)幸せなコーディング! –

+0

さらに、[このサイト](http://www.jsonschema2pojo.org/)はPOJOを作成するのに最適です –

関連する問題