Android Retrofit动态更改请求方法GET或POST

时间:2015-07-05 16:02:20

标签: android retrofit

我正在将我的Android应用程序从传统方式迁移到Retrofit。玩它的时候非常棒。

但是,我的应用程序的现有功能是它有一个全局设置页面,用于将请求方法更改为GET或POST请求(用于调试时的服务器端日志)。

 public static boolean isUsingPOST(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, 0);
    return prefs.getInt("http_request_method", 0) == 0;
}


 //Check for GET or POST
boolean useHttpPOST = isUsingPOST();

if (useHttpPOST == false) {     //GET
    HttpGet httpget = new HttpGet(URL);
    response = httpClient.execute(httpget);
} else {    //POST
    HttpPost httppost = new HttpPost(URL);
    response = httpClient.execute(httppost);
}

在改造中:我们在调用之前将请求方法定义为GET或POST:

@GET("/getVersion")
void getVersion(Callback<LoginResponse> callback);

我应该保留两种方法吗?? !!!

@GET("/getVersion")
void getVersionGET(Callback<LoginResponse> callback);
@POST("/getVersion")
void getVersionPOST(Callback<LoginResponse> callback);

有没有办法动态改变@GET或@POST ..我想我缺少非常基本的东西..请帮助我的朋友。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为您写的正确答案应该保留两种方法,因为在GET或POST之间切换不是您的http客户端层的责任。