1
我有一個方法,我用它發送我的所有請求到服務器。 而在這個方法中,我使用的是Builder類。 我只想知道如何根據函數的輸入通過「if」在每個請求中指定Builder的函數類型。 這裏是我的功能:如何選擇我要在Builder類中使用的函數的類型?
public String sendHttpRequest(String url, String parameters,String type) {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, parameters);
Request request = new Request.Builder()
.url(url)
/*what I'm going to do is somthing like this:
if(type.equal("get")){
.get()
}
if(type.equal("post")){
.post()
}
*/
.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
Response response = client.newCall(request).execute();
return response.body().string().toString();
} catch (IOException ex) {
}
return "";
}
所以,我應該怎麼辦?
這是偉大的!謝謝Nabin :) –