将名称值对附加到其他对

时间:2011-12-16 08:00:02

标签: java namevaluecollection

这是调用方法的代码 -

NameValuePair[] data = {
        new NameValuePair("first_name", firstName),
        new NameValuePair("last_name", lastName),
        new NameValuePair("email", email),
        new NameValuePair("company", organization),
        new NameValuePair("phone", phone)
    };
    SalesForceFormSubmissionUtil.submitForm(data,CONTACT_FOR_TYPE);

在被叫方法中 -

  public static void submitForm(NameValuePair[] givendata, String contactType) {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(Constants.SALESFORCE_URL);
    NameValuePair[] data = {
        new NameValuePair("oid", Constants.SALESFORCE_OID),
        new NameValuePair(Constants.SALESFORCE_CAMPAIGN_ID, contactType)
    };
    post.setRequestBody(data);

我想添加赠品和放大器数据并将其作为发布请求发送。你们可以建议我如何追加两者。

2 个答案:

答案 0 :(得分:0)

如果你只想组合这两个数组,你可以创建一个大小为combinedData + givenData.length的第三个数组(data.length),那么你将不得不复制这些元素来自您的源数组。

尽管如此,还有很多其他方法可以解决这个问题,例如;下面是使用ArrayList作为中间人/帮助者的实现。

String[] A1 = {"hello","world"};
String[] A2 = {"I","am","bored"};

...

ArrayList<String> temp = new ArrayList<String> (A1.length + A2.length);

temp.addAll (Arrays.asList (A1));
temp.addAll (Arrays.asList (A2));

String[] A3 = temp.toArray (new String[temp.size ()]);

...

for (int i=0; i < A3.length; ++i)
  System.out.println (A3[i]);

输出

hello
world
I
am
bored

答案 1 :(得分:0)

public static void submitForm(NameValuePair[] givendata, String contactType) {
   HttpClient client = new HttpClient();
   PostMethod post = new PostMethod(Constants.SALESFORCE_URL);
   post.setRequestBody(givendata);
   post.addParameters({
      new NameValuePair("oid", Constants.SALESFORCE_OID),
      new NameValuePair(Constants.SALESFORCE_CAMPAIGN_ID, contactType)
   });
   ...