Changeset 633 for AE/installer2/src/net/oni2/aeinstaller/backend/network
- Timestamp:
- Jan 18, 2013, 7:18:45 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
AE/installer2/src/net/oni2/aeinstaller/backend/network/DrupalJSONQuery.java
r621 r633 5 5 import java.io.InputStreamReader; 6 6 import java.io.UnsupportedEncodingException; 7 import java.util.ArrayList;8 import java.util.HashMap;9 import java.util.List;10 11 import net.oni2.aeinstaller.backend.depot.DepotConfig;12 7 13 8 import org.apache.http.HttpEntity; 14 9 import org.apache.http.HttpResponse; 15 import org.apache.http.NameValuePair;16 import org.apache.http.client.entity.UrlEncodedFormEntity;17 10 import org.apache.http.client.methods.HttpGet; 18 import org.apache.http.client.methods.HttpPost;19 11 import org.apache.http.client.methods.HttpRequestBase; 20 12 import org.apache.http.impl.client.DefaultHttpClient; 21 import org.apache.http.message.BasicNameValuePair;22 13 import org.apache.http.util.EntityUtils; 23 14 import org.json.JSONArray; … … 31 22 32 23 /** 33 * Execute an REST action through a HTTP POST query24 * Get the JSON array found at the given url 34 25 * 35 * @param resource 36 * Resource to run on 37 * @param action 38 * Action to call 39 * @param postData 40 * Fieldname / value pairs to include in POST data 41 * @return JSON structure of item 26 * @param url 27 * URL to look at for the JSON data 28 * @return JSON array of data 42 29 * @throws Exception 43 * on HTTP error30 * On HTTP status code <200 / >299 44 31 */ 45 public static JSONArray postAction(String resource, String action, 46 HashMap<String, String> postData) throws Exception { 47 try { 48 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 49 for (String key : postData.keySet()) { 50 nvps.add(new BasicNameValuePair(key, postData.get(key))); 51 } 52 HttpEntity data = new UrlEncodedFormEntity(nvps); 53 return executeQuery(DepotConfig.getDepotApiUrl() + resource + "/" + action 54 + ".json", data); 55 } catch (UnsupportedEncodingException e) { 56 e.printStackTrace(); 57 } 58 return null; 59 } 60 61 /** 62 * Execute an REST references query through a HTTP GET query 63 * 64 * @param resource 65 * Resource to run on 66 * @param index 67 * Index of item to get the references from 68 * @param refName 69 * Name of references type 70 * @return JSON structure of item 71 * @throws Exception 72 * on HTTP error 73 */ 74 public static JSONArray getReferences(String resource, int index, 75 String refName) throws Exception { 76 return executeQuery( 77 DepotConfig.getDepotApiUrl() + resource + "/" + Integer.toString(index) + "/" 78 + refName + ".json", null); 79 } 80 81 /** 82 * Execute an REST item query through a HTTP GET query 83 * 84 * @param resource 85 * Resource to run on 86 * @param index 87 * Index of item to get 88 * @param parameters 89 * Parameters to pass (must start with ampersand "&") 90 * @return JSON structure of item 91 * @throws Exception 92 * on HTTP error 93 */ 94 public static JSONArray getItem(String resource, int index, 95 String parameters) throws Exception { 96 return executeQuery( 97 DepotConfig.getDepotApiUrl() + resource + "/" + Integer.toString(index) 98 + ".json" + parameters, null); 99 } 100 101 /** 102 * Execute an REST index query through a HTTP GET query 103 * 104 * @param resource 105 * Resource to run on 106 * @param page 107 * Number of page to get (for limited results, e.g. nodes), -1 to 108 * ignore 109 * @param pagesize 110 * Maximum number of elements to return 111 * @return JSON structure of item 112 * @throws Exception 113 * on HTTP error 114 */ 115 public static JSONArray getIndex(String resource, int page, int pagesize) 116 throws Exception { 117 String pageN = ""; 118 if (page >= 0) 119 pageN = "&page=" + Integer.toString(page); 120 String pagesizeN = ""; 121 if (pagesize >= 0) 122 pagesizeN = "&pagesize=" + Integer.toString(pagesize); 123 return executeQuery(DepotConfig.getDepotApiUrl() + resource + ".json" + pageN 124 + pagesizeN, null); 125 } 126 127 private static JSONArray executeQuery(String url, HttpEntity postData) 128 throws Exception { 32 public static JSONArray executeQuery(String url) throws Exception { 129 33 BufferedReader input = null; 130 34 HttpRequestBase httpQuery = null; … … 132 36 try { 133 37 DefaultHttpClient httpclient = new DefaultHttpClient(); 134 if (postData == null) { 135 httpQuery = new HttpGet(url); 136 } else { 137 httpQuery = new HttpPost(url); 138 ((HttpPost) httpQuery).setEntity(postData); 139 } 38 httpQuery = new HttpGet(url); 140 39 141 40 HttpResponse response = httpclient.execute(httpQuery);
Note:
See TracChangeset
for help on using the changeset viewer.