Package api.http

Source Code of api.http.HttpQuery

package api.http;

import api.KeyValuePair;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class HttpQuery{

  private List<KeyValuePair> pairs = new ArrayList<KeyValuePair>();

  /**
   * constructs this object.
   */
  public HttpQuery(){
  }

  /**
   * adds the key and value into this object.
   *
   * @param key the key of KeyValuePair object.
   * @param value the value of KeyValuePair object.
   */
  public void add(String key, String value){
    this.pairs.add(new KeyValuePair(key, value));
  }

  /**
   * returns the string represent this object.
   *
   * @return the string represent this object.
   */
  @Override
  public String toString(){
    StringBuilder sbr = new StringBuilder();
    for (int i = 0; i < this.pairs.size(); i++){
      KeyValuePair pair = this.pairs.get(i);
      if (i > 0){
        sbr.append("&");
      }
      try{
        sbr.append(URLEncoder.encode(pair.key, "UTF-8"));
      } catch (UnsupportedEncodingException ex){
        sbr.append(pair.key);
      }
      sbr.append("=");
      try{
        sbr.append(URLEncoder.encode(pair.value, "UTF-8"));
      } catch (UnsupportedEncodingException ex){
        sbr.append(pair.value);
      }
    }
    return sbr.toString();
  }
}
TOP

Related Classes of api.http.HttpQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.