我正在开发一个java应用程序在google购物市场中添加项目的解决方案。下面的教程是一个真正的混乱,因为缺少类的最后一个版本,但在设置1.5-beta与maven之后,我设法使基本的例子,以正确的类型检查所有的类。然而,我仍然可以让它做一个简单的任务,添加一个项目。我不是发布整个代码,而是发布仅创建Product提要的部分。
private Product createProduct(String id) {
Product product = new Product();
product.title = "Red wool sweater";
product.content =
new Content(
"text",
"Comfortable and soft, this sweater will keep you warm on those "
+ "cold winter nights. Red and blue stripes.");
product.appControl = new AppControl();
product.appControl.addRequiredDestination("ProductAds");
product.externalId = id;
product.lang = "it";
product.country = "it";
product.condition = "nuovo";
product.price = new Price("EUR", new BigDecimal("12.99"));
product.googleProductCategory = "Sporting Goods > Exercise & Fitness > Cardio Machines > Exercise Bikes";
// add a link
Link link = new Link();
link.rel = "alternate";
link.href = homepage + "item1-info-page.html";
link.type = "text/html";
product.links.add(link);
//shipping
List<com.google.api.client.sample.structuredcontent.model.Shipping> shipping = new ArrayList<com.google.api.client.sample.structuredcontent.model.Shipping>();
com.google.api.client.sample.structuredcontent.model.Shipping s = new com.google.api.client.sample.structuredcontent.model.Shipping();
s.shippingCountry="IT";
Price p = new Price();
p.unit = "EUR";
p.value = new BigDecimal("0");
s.shippingPrice= p;
//s.shippingRegion=""
s.shippingService = "Speedy Shipping - Ground";
shipping.add(s);
product.shippingRules = shipping;
//tax
//Useless in italy
// set image links
List<String> imageLinks = new ArrayList<String>();
imageLinks.add("http://www.example.com/image1.jpg");
imageLinks.add("http://www.example.com/image2.jpg");
product.imageLinks = imageLinks;
return product;
}
我成功地获取了html请求的内容:
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:sc="http://schemas.google.com/structuredcontent/2009" xmlns:scp="http://schemas.google.com/structuredcontent/2009/products"><app:control><sc:required_destination dest="ProductAds" />
</app:control>
<content type="text">
Comfortable and soft, this sweater will keep you warm on those cold winter nights. Red and blue stripes.</content>
<link href="http://www.joinstore.it/item1-info-page.html" rel="alternate" type="text/html" />
<sc:adult>false</sc:adult><sc:content_language>it</sc:content_language>
<sc:id>1234567</sc:id>
<sc:image_link>http://www.example.com/image1.jpg</sc:image_link>
<sc:image_link>http://www.example.com/image2.jpg</sc:image_link>
<sc:target_country>it</sc:target_country>
<scp:condition>nuovo</scp:condition>
<scp:featured_product>false</scp:featured_product>
<scp:google_product_category>Sporting Goods > Exercise & Fitness > Cardio Machines > Exercise Bikes</scp:google_product_category>
<scp:price unit="EUR">12.99</scp:price>
<scp:shipping><scp:shipping_country>IT</scp:shipping_country>
<scp:shipping_price unit="EUR">0</scp:shipping_price>
<scp:shipping_service>Speedy Shipping - Ground</scp:shipping_service></scp:shipping>
<title>Red wool sweater</title>
</entry>
按照一个家伙在google-api论坛的建议,我已经设置了网站链接,航运和税收在商人中心。
我真的不知道该怎么办,有人知道发生了什么事吗?
最后我找到了一个解决方案,这要感谢api的google组。在我能够使用以下代码获得http请求的内容之后:
// HTTP request
HttpRequest request = requestFactory.buildPostRequest(new GoogleUrl(url), atomContent);
File f = new File(Main.BASE_FOLDER_PATH + "ciao.xml");
FileOutputStream out = new FileOutputStream(f);
request.getContent().writeTo(out);
out.close();
HttpResponse re = null;
re = request.execute();
我使用内容api演示工具通过复制粘贴原始xml来发送插入请求。从工具中,我能够得到响应中的错误,但是我注意到,我通过使用java api得到的响应是不同的,因为HttpClient类如果不能解析产品就会产生异常。