cancel
Showing results for 
Search instead for 
Did you mean: 

Error in upload

Highlighted
Occasional Contributor

Error in upload

Hello,

 

I'm trying to create a Bulk Import class in Java but when I run the program I get this error:

Exception in thread "main" com.constantcontact.exceptions.service.ConstantContactServiceException: com.constantcontact.exceptions.service.ConstantContactServiceException: Response came with errors from WebService. Please see error info.
at com.constantcontact.services.activities.BulkActivitiesService.addContacts(BulkActivitiesService.java:102)
at org.cbservices.BulkMemberUpload.UploadMembers(BulkMemberUpload.java:111)
at org.cbservices.BulkMemberUpload.main(BulkMemberUpload.java:155)
Caused by: com.constantcontact.exceptions.service.ConstantContactServiceException: Response came with errors from WebService. Please see error info.
at com.constantcontact.util.ConstantContactExceptionFactory.createServiceException(ConstantContactExceptionFactory.java:12)
at com.constantcontact.services.activities.BulkActivitiesService.addContacts(BulkActivitiesService.java:98)

 

I don't under stand what the issue is. If someone could help me I would appreciate it. Here is my code I've highlight the spots where the errors are occuring in red:

 

 

 

package org.cbservices;

import com.constantcontact.services.activities.BulkActivitiesService;
import com.constantcontact.components.activities.contacts.request.ContactData;
import com.constantcontact.components.activities.contacts.request.CustomField;
import com.constantcontact.components.activities.contacts.request.AddContactsRequest;
import com.constantcontact.components.contacts.ContactList;
import com.constantcontact.exceptions.ConstantContactException;
import com.constantcontact.exceptions.component.ConstantContactComponentException;
import com.constantcontact.exceptions.service.ConstantContactServiceException;

 


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BulkMemberUpload
{
public BulkMemberUpload()
{

}

public static void UploadMembers(ArrayList alMemberId, ArrayList alFirstName,
ArrayList alLastName, ArrayList alEmailId,
String strAPIKey, String strAccessToken) throws ConstantContactServiceException
{
// Create Constant Contact object
BulkActivitiesService objConstantContact = new BulkActivitiesService(strAPIKey, strAccessToken);

// Create AddContactsRequest object
AddContactsRequest objAddContactsRequest = new AddContactsRequest();

// Create new list of contact data
List<ContactData> lsContactData = new ArrayList<ContactData>();

// Create list of columns
List<String> lsColumnName = new ArrayList<String>();

// Create object for contact list
List<String> contactlist = new ArrayList<String>();

// ContactList object
ContactList objContactList = new ContactList();

for(int i=0; i < alMemberId.size(); i++)
{
// Create new contact data object
ContactData objContactData = new ContactData();

// Set Email Id
List<String> lsEmailAddresses = new ArrayList<String>();
lsEmailAddresses.add(alEmailId.get(i).toString());
objContactData.setEmailAddresses(lsEmailAddresses);

// Set First Name
objContactData.setFirstName(alFirstName.get(i).toString());

// Set Last Name
objContactData.setLastName(alLastName.get(i).toString());

//Set Member Id
CustomField objCustomField = new CustomField();
List<CustomField> lsCustomField = new ArrayList<CustomField>();
objCustomField.setName("CUSTOM FIELD 1");
objCustomField.setValue(alMemberId.get(i).toString());
lsCustomField.add(objCustomField);
//objContactData.setCustomFields(lsCustomField);

CustomField objCustomField2 = new CustomField();
//List<CustomField> lsCustomField = new ArrayList<CustomField>();
objCustomField2.setName("CUSTOM FIELD 2");
objCustomField2.setValue(alMemberId.get(i).toString());
lsCustomField.add(objCustomField2);
objContactData.setCustomFields(lsCustomField);


// Add contact to contact list
lsContactData.add(objContactData);

} // End of for


// Add column names for list
lsColumnName.add("EMAIL");
lsColumnName.add("FIRST NAME");
lsColumnName.add("LAST NAME");
lsColumnName.add("CUSTOM FIELD 1");

// Set List id
contactlist.add("4");

// Set list name for upload
objAddContactsRequest.setLists(contactlist);

// Set column names
objAddContactsRequest.setColumnNames(lsColumnName);

// Import data into AddContactsRequest
objAddContactsRequest.setImportData(lsContactData);


// Upload members
try {
System.out.println(objAddContactsRequest.toJSON());
objConstantContact.addContacts(objAddContactsRequest);
} catch (ConstantContactComponentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main (String[] args) throws ConstantContactException
{
String strAPIKey = "REDACTED";
String strAccessToken = "REDACTED";

ArrayList alMemberId = new ArrayList();
ArrayList alFirstName = new ArrayList();
ArrayList alLastName = new ArrayList();
ArrayList alEmailId = new ArrayList();

try

{
FileReader fr = new FileReader("N:/Data/Excel/Emails.csv");
BufferedReader br = new BufferedReader(fr);
String stringRead = br.readLine();
double counter = 0001;

while( stringRead != null )
{
String[] elements = stringRead.split(",");

alMemberId.add(counter);
alFirstName.add(elements[0].toString());
alLastName.add(elements[1].toString());
alEmailId.add(elements[2].toString());

counter++;
// read the next line
stringRead = br.readLine();
}
}
catch(IOException ioe)
{
ioe.getMessage();
}
UploadMembers(alMemberId, alFirstName, alLastName, alEmailId, strAPIKey, strAccessToken);
}

}

 

2 REPLIES 2
Occasional Visitor

Re: Error in upload

You can get more error detail by catching the exception and then outputting the error messages:

 

 

try {

// your code here


} catch (ConstantContactServiceException e){
            for(RawApiRequestError error: e.getErrorInfo()){
                System.out.println(error.getErrorMessage());
            }
}

Took me a while to figure this out... cant find any documentation for the java sdk anywhere

Honored Contributor

Re: Error in upload

Hello,

 

While this functionality of the exceptions is not explicity doumented, you can find an example for the usage of these exceptions in the sample code included with our SDK. One such example can be found here: https://github.com/constantcontact/java-sdk/blob/master/AndroidExamples/AddOrUpdateContacts/src/com/...

 

If you do have any questions about the Java SDK, please feel free to ask!

 

Sincerely,

Elijah G.
API Support Engineer