2017-09-17 38 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 
    );