使用spring数据rest,Retrofit返回内容长度0

时间:2017-09-17 12:37:33

标签: heroku spring-boot retrofit2

我正在使用spring boot应用程序,并在heroku上部署了spring数据。我有一个/ api / userDatas端点,POST请求可以在其上创建实体。我使用Postman测试了它并且它被创建了。

现在我在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 个答案:

答案 0 :(得分:1)

找到解决方案。

在ServeNetworking中添加了Accept标头:

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