在Retrofit连接中出错

时间:2017-06-05 12:35:15

标签: java android http-post

我的界面

@POST("/insert.php")
void  login(Callback<Response> callback);

Java代码

Retrofit adapter = new Retrofit.Builder()
                       .baseUrl(ROOT_URL) //Setting the Root URL
                       .addConverterFactory(GsonConverterFactory.create())
                       .build(); //Finally building the adapter

Register_Retrofit api = adapter.create(Register_Retrofit.class);


api.login( new Callback<Response>() {
    public void onResponse(Call<Response> call, Response<Response> response) {

    }

    public void onFailure(Call<Response> call, Throwable t) {

    }

});

1 个答案:

答案 0 :(得分:0)

您的登录方法返回void,因此您需要像这样定义:

@POST("/insert.php")
Call<Void> login();

然后,要调用login方法,请尝试:

Retrofit adapter = new Retrofit.Builder()
                       .baseUrl(ROOT_URL) //Setting the Root URL
                       .addConverterFactory(GsonConverterFactory.create())
                       .build(); //Finally building the adapter

Register_Retrofit api = adapter.create(Register_Retrofit.class);
Call<Void> loginCall = api.login();
loginCall.enqueue(new Callback<Void>() { 
     public void onResponse(Call<Void> call, Response<Void> response) { 
          ... 
     } 

     public void onFailure(Call<Void> call, Throwable t) { 
          ... 
     }
});