summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-1.x/samples/store-android/src/services/CatalogProxy.java
blob: 3ce9459844c3b7c98c6beb098d558bb17d85debf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package services;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import services.json.rpc.JSONRpc;

public class CatalogProxy implements Catalog {
	private static final String jsonRPCServiceURI = "http://192.168.1.104:8080/Catalog";
    private static final String jsonRPCRequest = "{\"id\": 3, \"method\": \"Service.get\", \"params\": []}";
	
    private List<Item> catalog = new ArrayList<Item>();
    
    public CatalogProxy() {
    	initialize();
    }
   
	public void initialize() {
		JSONObject json = null; 
			
		try {
			json = JSONRpc.invoke(jsonRPCServiceURI, jsonRPCRequest);
			
			if(json == null) {
				return; 
			}
			
			JSONArray result = json.getJSONArray("result");
			for(int i = 0; i < result.length(); i++) {
				Item item = new Item();
				item.setName(result.getJSONObject(i).getString("name"));
				item.setPrice(result.getJSONObject(i).getString("price"));
				
				catalog.add(item);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public Item[] get() {
		Item[] catalogArray = new Item[catalog.size()];
		catalog.toArray(catalogArray);
		return catalogArray;
	}
	
}