2017-08-31 7 views
1

ApiClient.javaクラスを返す -レトロフィット404 responeコード

public class ApiClient { 

    public static final String BASE_URL = "https://jsonplaceholder.typicode.com/"; 
    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; 
    } 
} 

インタフェース - 活性のonCreateで

public interface ApiInterface { 

    @POST("posts/1") 
    Call<ModelClass> savePost(); 
} 

- 私はGradleの中に含ま

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); 

     Call<ModelClass> call = apiService.savePost(); 
     call.enqueue(new Callback<ModelClass>() { 
      @Override 
      public void onResponse(Call<ModelClass>call, Response<ModelClass> response) { 

       Log.i(TAG,"response is "+response.body()); 
       Log.i(TAG,"response code is "+response.code()); 
      } 

      @Override 
      public void onFailure(Call<ModelClass>call, Throwable t) { 
       // Log error here since request failed 
       Log.i(TAG, t.toString()); 
      } 
     }); 

依存 -

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

なぜretrofitが404の応答コードを返すのですか?上記のコードに問題はありますか? ありがとうございます。

+0

インターネットのアクセス権はありますか? – Zoe

+0

はい、すでに追加されています。 – sanil

+0

インターネットに接続していますか?デバイスは動作しているアクティブな接続を持っていますか? Android OSは接続を切断します(接続していますが、データを送受信できません)。 – Zoe

答えて

1

あなたのコードは、apiで問題はありません。私はその404は、ので、あなたのサーバー

しかし、その、体として空のJSONオブジェクトを示していない確認してくださいが見つかりませんでした示すPOSTMANにPOSTメソッドであなたのAPIを打つ ので、多分そこに欠落しているパラメータがあり、あなたはこのことを示すために404のステータスを使用していますこれは悪い練習であり、このAPIが存在しない場合にのみ表示され、409,422のような他のエラーコードを使用すると、応答についてより明確になります。

関連する問題