public boolean checkResponse(HttpEntity entity, Consumer c) throws Exception { String r = EntityUtils.toString(entity); APIConnection api = new APIConnection(); JSONObject json = new JSONObject(r); String id = (String) json.get(api.getConnection_key()); return id.equalsIgnoreCase("ok"); }
public boolean disconnectProducer(Consumer c) { boolean result = false; APIConnection api = new APIConnection(); HttpResponse response = api.get(api.getConsumerDisconnect(), c.getConnection_device_id()); int code = api.getResponse(response); if (code == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { try { result = checkResponse(entity, c); } catch (Exception e) { return result; } } } return result; }
public boolean connectProducer(Consumer c) { boolean result = false; APIConnection api = new APIConnection(); HttpResponse response = api.get(api.getConsumerConnect(), c.getConnection_device_id()); int code = api.getResponse(response); System.out.println("Connection to producer request status:" + code); if (code == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { try { result = checkResponse(entity, c); } catch (Exception e) { System.out.println("Exception caught"); return result; } } } return result; }
/** * Requests mobile device for the producer returns the HTTP response code and 0 if failed to * execute post. * * @param c * @return */ public int requestProducer(Consumer c) { int result = 0; APIConnection api = new APIConnection(); String param = c.consumerToJSON(); System.out.println(param); HttpResponse response = api.post(api.getConsumerRequest(), param); // HttpResponse response = api.post(api.getConsumerRequest(), c.consumerToJSON()); try { System.out.println( response.toString()); // ///////////////remove just to show response for now result = api.getResponse(response); if (result == 200) { HttpEntity entity = response.getEntity(); JSONArray jsonArray = getJSONArray(entity); if (jsonArray == null) { return 0; } else { // currently takes first response and uses that port/ip JSONObject json = jsonArray.getJSONObject(0); System.out.println(json.toString()); String port = (String) json.get(api.getPort_key()); String ip = (String) json.get(api.getIp_key()); String device = (String) json.get(api.getDevice_id_key()); c.setConnection_ip(ip); c.setConnection_port(port); c.setConnection_device_id(device); return result; } } } catch (Exception e) { System.out.println("Exception thrown"); e.printStackTrace(); return result; } return result; }