2017-09-17 37 views
1

我正在使用彈簧啓動應用程序,將spring數據rest部署在heroku上。我有一個/ api/userDatas端點,可以通過POST請求創建一個實體。我用郵差測試了它,並且它被創建。Retrofit返回的內容長度爲0,其餘爲彈簧數據

現在我在android上使用retrofit來執行相同的功能。但問題是實體是在服務器上創建的,但onFailure()總是被調用。我已經調試,發現內容的長度始終爲0

CREATEUSER

private void createUser() { 
     Gson gson = new GsonBuilder() 
       .setLenient() 
       .create(); 

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

     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl(ServeNetworking.ServeURLS.getBaseURL()) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(client) 
       .build(); 


     ServeNetworking serveNetworking = retrofit.create(ServeNetworking.class); 
     Call<UserData> getUserByIdResponseCall = serveNetworking.socialConnectAPI(userData); 
     getUserByIdResponseCall.enqueue(new Callback<UserData>() { 
      @Override 
      public void onResponse(Call<UserData> call, Response<UserData> response) { 
       Log.d("getUserById", "onResponse"); 
       Toast.makeText(OTPVerificationActivity.this, "userId : onResponse", Toast.LENGTH_LONG).show(); 
       /** 
       * Save data in local db and navigate to map screen. 
       */ 

       ActivityAnimationUtils.presentActivity(OTPVerificationActivity.this, MapActivity.class); 

       try{ 
        DBManager.getDBManager(OTPVerificationActivity.this).setIsVerified(true); 
       } catch (Exception e){ 
        e.printStackTrace(); 
       } 

      } 

      @Override 
      public void onFailure(Call<UserData> call, Throwable t) { 
       Log.d("getUserById", "onFailure"); 
       Utils.showSnackBar(OTPVerificationActivity.this,"Somethign went wrong", root); 
      } 
     }); 
    } 

和接口:

public interface ServeNetworking { 

    @POST("/api/userDatas") 
    @Headers({ "Content-Type: application/json;charset=UTF-8"}) 
    Call<UserData> socialConnectAPI(
      @Body UserData user 
    ); 

    @GET("/api/userDatas/{userId}") 
    @Headers({ "Content-Type: application/json; charset=utf-8"}) 
    Call<UserData> getUser(
      @Path("userId") String userId 
    ); 


    /** 
    * this class is used to get the donor base urls... 
    */ 
    class ServeURLS{ 
     private static String baseURL="https://fabrimizer-serve.herokuapp.com"; 

     public static String getBaseURL() { 
      return baseURL; 
     } 

    } 
} 

我收到以下錯誤:

java.io.EOFException: End of input at line 1 column 1 path $ 

回答

1

發現解。

加入ServeNetworking Accept首部:

@POST("/api/userDatas") 
    @Headers({ "Content-Type: application/json,"Accept: application/json"}) 
    Call<UserData> socialConnectAPI(
      @Body UserData user 
    );