SDK
...
ZoneID is different for every app so please contact RMC for support. Recommendation widgets like "Alternative Products" require the product code being viewed as a parameter.
The specification of the callback function is:
Code Block |
---|
language | java |
---|
linenumbers | true |
---|
|
VisilabsTargetRequest targetRequest = Visilabs.CallAPI().buildTargetRequest(zoneID, "productCode");
targetRequest.executeAsync(callback); |
After the request is completed, depending on the status of the request either success or failure callback function will be called, therefore based on your requirements, you need to code your callback functions as shown below:
Code Block |
---|
language | java |
---|
linenumbers | true |
---|
|
VisilabsTargetCallback callback = new VisilabsTargetCallback() {
@Override
public void success(VisilabsResponse response) {
try{
JSONArray array = response.getArray();
if(array != null) {
ArrayList<Product> recommendations = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject obj = (JSONObject)array.get(i);
Product product = new Product();
product.setProductCode(obj.getString("code"));
product.setProductName(obj.getString("title"));
product.setMediumImageUrl(obj.getString("img"));
product.setProductURL(obj.getString("dest_url"));
product.setBrand(obj.getString("brand"));
product.setOriginalPrice(obj.getDouble("price"));
product.setDiscountedPrice(obj.getDouble("dprice"));
product.setCurrency(obj.getString("cur"));
product.setDiscountCurrency(obj.getString("dcur"));
product.setFreeShipping(obj.getBoolean("freeshipping"));
product.setShippingOnSameDay
(obj.getBoolean("samedayshipping"));
product.setRating(obj.getInt("rating"));
product.setComment(obj.getInt("comment"));
product.setDiscountRate(obj.getDouble("discount"));
product.setAttr1(obj.getString("attr1"));
product.setAttr2(obj.getString("attr2"));
product.setAttr3(obj.getString("attr3"));
product.setAttr4(obj.getString("attr4"));
product.setAttr5(obj.getString("attr5"));
recommendations.add(product);
}
}
}catch (Exception ex){
showToast(ex.getMessage());
Log.e(LOG_TAG, ex.getMessage(), ex);
}
}
@Override
public void fail(VisilabsResponse response) {
showToast(response.getErrorMessage());
Log.d(LOG_TAG, rawResponse);
}
}; |
...
Product recommendations are array of products with product details like brand, price etc. After you receive the product recommendations you need to display recommended products according to your mobile applications’s requirements. This section outlines how to receive recommendations as an array.
Create a VisilabsTargetRequest object:
Code Block |
---|
language | applescript |
---|
title | Objective-C |
---|
|
VisilabsTargetRequest *request=[[Visilabs callAPI] buildTargetRequest: @"ZoneID" withProductCode: @"ProductCode"]; |
Code Block |
---|
language | applescript |
---|
title | Swift |
---|
|
let targetRequest = Visilabs.callAPI().buildTargetRequest("ZoneID", withProductCode: "ProductCode") |
After the request is completed, depending on the status of the request either success or failure callback function will be called, therefore based on your requirements, you need to code your callback functions as shown below:
Code Block |
---|
language | applescript |
---|
title | Objective-C |
---|
|
void (^ successBlock)(VisilabsResponse *) = ^(VisilabsResponse * response) { NSLog(@"Response: %@", response.rawResponseAsString); NSArray *parsedArray = [response responseArray]; if(parsedArray){ for (NSObject * object in parsedArray) { if([object isKindOfClass:[NSDictionary class]]){ NSDictionary *product = (NSDictionary*)object; NSString *title = [product objectForKey:@"title"]; NSString *img = [product objectForKey:@"img"]; NSString *code = [product objectForKey:@"code"]; NSString *destURL = [product objectForKey:@"dest_url"]; NSString *brand = [product objectForKey:@"brand"]; double price = [[product objectForKey:@"price"] doubleValue]; double discountedPrice = [[product objectForKey:@"dprice"] doubleValue]; NSString *currency = [product objectForKey:@"cur"]; NSString *discountCurrency = [product objectForKey:@"dcur"]; int rating = [[product objectForKey:@"rating"] intValue]; int comment = [[product objectForKey:@"comment"] intValue]; double discount = [[product objectForKey:@"discount"] doubleValue]; BOOL freeShipping = [[product objectForKey:@"freeshipping"] boolValue]; BOOL sameDayShipping = [[product objectForKey:@"samedayshipping"] boolValue]; NSString *attr1 = [product objectForKey:@"attr1"]; NSString *attr2 = [product objectForKey:@"attr2"]; NSString *attr3 = [product objectForKey:@"attr3"]; NSString *attr4 = [product objectForKey:@"attr4"]; NSString *attr5 = [product objectForKey:@"attr5"]; } } } }; void (^ failBlock)(VisilabsResponse *) =^(VisilabsResponse * response){ NSLog(@"Failed to call. Response = %@", [actFailRes.error description]); }; |
Code Block |
---|
language | applescript |
---|
title | Swift |
---|
|
let successBlock:(VisilabsResponse!) -> Void = {response in print("Response:\(response.rawResponseAsString)") for object:AnyObject in response.responseArray{ if(object.isKindOfClass(NSDictionary)){ let productObject = object as! NSDictionary; let title = productObject.objectForKey("title") as! String let img = productObject.objectForKey("img") as! String let code = productObject.objectForKey("code") as! String let dest_url = productObject.objectForKey("dest_url") as! String let brand = productObject.objectForKey("brand") as! String let price = productObject.objectForKey("price") as! Double let discountedPrice = productObject.objectForKey("dprice") as! Double let currency = productObject.objectForKey("cur") as! String let discountCurrency = productObject.objectForKey("dcur") as! String let rating = productObject.objectForKey("rating") as! Int let comment = productObject.objectForKey("comment") as! Int let discount = productObject.objectForKey("discount") as! Double let freeShipping =productObject.objectForKey("freeshipping") as! Bool let sameDayShipping=productObject.objectForKey("samedayshipping") as! Bool let attr1 = productObject.objectForKey("attr1") as! String let attr2 = productObject.objectForKey("attr2") as! String let attr3 = productObject.objectForKey("attr3") as! String let attr4 = productObject.objectForKey("attr4") as! String let attr5 = productObject.objectForKey("attr5") as! String } } } let failBlock: (VisilabsResponse!) -> Void = {response in VisilabsHelper.targetLog(response.targetURL) print("Visilabs target call failed.Error=\(response.rawResponseAsString)") } |