2015-09-07 46 views
1

我有下面的代碼片段:Java:如何將x-times方法附加到對象實例?

// Create URI object first 
     JSONObject requestParamsObject = null; 
     if (requestParams != null) { 
      requestParamsObject = new JSONObject(requestParams); 
     } else { 
      Logger.i("Params variable is null"); 
      Uri.Builder builder = new Uri.Builder(); 
      builder.scheme("http") 
       .encodedAuthority(url) 
       .appendQueryParameter("username", "test") 
       .appendQueryParameter("password", "password") 
       .appendQueryParameter("version", "2.3"); 
      url = URLDecoder.decode(builder.build().toString(), "UTF-8"); 
      Logger.i(url); 
     } 

我想用這些方法:

.appendQueryParameter("username", "test") 
        .appendQueryParameter("password", "password") 
        .appendQueryParameter("version", "2.3"); 

要追加使用的循環方法。

FXP:

// Set params to request 
     Map<String, String> mRequestParams = new HashMap<>(); 
     mRequestParams.put(Constants.Login.PARAM_URL_KEY_USERNAME, username); 
     mRequestParams.put(Constants.Login.PARAM_URL_KEY_PASSWORD, password); 
     mRequestParams.put(Constants.Login.PARAM_URL_KEY_APP_VERSION, "2.3" 

要通過類似的方式像這樣使用:

mRequestParams[i] 
.appendQueryParameter(RequestParams[i].key, RequestParams[i].value) 

我該怎麼辦呢?

+0

如何使用遞歸,你在你的數據在傳遞一些集合使用,可能的HashMap – Arunkumar

+0

請你添加一些例子嗎? – redrom

回答

1

如何:

Uri.Builder builder = new Uri.Builder(); 
builder.scheme("http").encodedAuthority(url); 
for (Map.Entry<String, String> entry : mRequestParams.entrySet()) { 
    builder.appendQueryParam(entry.key, entry.value); 
} 
url = ... 
相關問題