View Javadoc
1   package com.kdt.test;
2   
3   import java.io.BufferedReader;
4   import java.io.InputStreamReader;
5   import java.util.ArrayList;
6   import java.util.HashMap;
7   import java.util.List;
8   
9   import org.apache.http.HttpResponse;
10  
11  import com.kdt.api.KdtApiClient;
12  
13  /*
14   * 这是个例子
15   */
16  public class KDTApiTest {
17  	private static final String APP_ID = "app_id"; //这里换成你的app_id
18  	private static final String APP_SECRET = "app_secret"; //这里换成你的app_secret
19  	
20  	public static void main(String[] args){
21  		sendGet();
22  		sendPost();
23  	}
24  	
25  	/*
26  	 * 测试获取单个商品信息
27  	 */
28  	private static void sendGet(){
29  		String method = "kdt.item.get";
30  		HashMap<String, String> params = new HashMap<String, String>();
31  		params.put("num_iid", "2651514");
32  		
33  		KdtApiClient kdtApiClient;
34  		HttpResponse response;
35  		
36  		try {
37  			kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
38  			response = kdtApiClient.get(method, params);
39  			System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
40  			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
41  			StringBuffer result = new StringBuffer();
42  			String line = "";
43  			while ((line = bufferedReader.readLine()) != null) {
44  				result.append(line);
45  			}
46  
47  			System.out.println(result.toString());
48  		} catch (Exception e) {
49  			e.printStackTrace();
50  		}
51  	}
52  	
53  	/*
54  	 * 测试获取添加商品
55  	 */
56  	private static void sendPost(){
57  		String method = "kdt.item.add";
58  		HashMap<String, String> params = new HashMap<String, String>();
59  		params.put("price", "999.01");
60  		params.put("title", "测试商品");
61  		params.put("desc", "这是一个号商铺");
62  		params.put("is_virtual", "0");
63  		params.put("post_fee", "10.01");
64  		params.put("sku_properties", "");
65  		params.put("sku_quantities", "");
66  		params.put("sku_prices", "");
67  		params.put("sku_outer_ids", "");
68  		String fileKey = "images[]";
69  		List<String> filePaths = new ArrayList<String>();
70  		filePaths.add("/Users/xuexiaozhe/Desktop/1.png");
71  		filePaths.add("/Users/xuexiaozhe/Desktop/2.png");
72  		
73  		KdtApiClient kdtApiClient;
74  		HttpResponse response;
75  		
76  		try {
77  			kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
78  			response = kdtApiClient.post(method, params, filePaths, fileKey);
79  			System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
80  			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
81  			StringBuffer result = new StringBuffer();
82  			String line = "";
83  			while ((line = bufferedReader.readLine()) != null) {
84  				result.append(line);
85  			}
86  
87  			System.out.println(result.toString());
88  		} catch (Exception e) {
89  			e.printStackTrace();
90  		}
91  	}
92  }