public GetRecurringPaymentsProfileDetailsResposta getRecurringPaymentsProfileDetails( Map<String, String[]> parametros) throws IllegalStateException { StringBuilder param = new StringBuilder(); GetRecurringPaymentsProfileDetailsResposta resp = null; try { HttpsURLConnection conn = Util.getConexaoHttps((String) parametros.get("NAOENVIAR_ENDPOINT")[0]); logger.info("Parametros da chamada GetRecurringPaymentsProfileDetails:"); for (Map.Entry<String, String[]> item : parametros.entrySet()) { if (podeEnviarParametro(item.getKey(), item.getValue()[0])) { param.append(item.getKey() + "=" + URLEncoder.encode(item.getValue()[0], "UTF-8") + "&"); logger.info(" " + item.getKey() + ": " + item.getValue()[0] + "&"); } } OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); logger.info("Chamada a: " + conn.getURL() + " com os parametros: " + param.toString()); writer.write(param.toString()); writer.flush(); writer.close(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); param = null; param = new StringBuilder(); BufferedReader reader = new BufferedReader(in); String data; logger.info("Retorno da chamada: "); while ((data = reader.readLine()) != null) { param.append(data); } /*if (data.contains("TOKEN")) { param.append("=================================================="); }*/ data = param.toString(); GetRecurringPaymentsProfileDetailsParser parser = GetRecurringPaymentsProfileDetailsParser.getInstance(); resp = parser.parse(data); logger.info(data); } catch (IOException ex) { logger.fatal( "Erro ao executar GetRecurringPaymentsProfileDetails: " + ex.getLocalizedMessage(), ex); } return resp; }
/** * Executa o metodo SetExpressCheckout * * @param parametros Parametros recebido do formulario ou do sistema do cliente */ public SetExpressCheckoutResposta setExpressCheckout(Map<String, String[]> parametros) throws IllegalStateException { StringBuilder param = new StringBuilder(); SetExpressCheckoutResposta resp = null; if (this.getCredenciais() == null) { throw new IllegalStateException("As credencais do merchant nao foram informadas."); } try { HttpsURLConnection conn = Util.getConexaoHttps((String) parametros.get("NAOENVIAR_ENDPOINT")[0]); logger.info("Parametros da chamada:"); logger.info("Conectando-se a " + conn.getURL()); for (Map.Entry<String, String[]> item : parametros.entrySet()) { if (podeEnviarParametro(item.getKey(), item.getValue()[0])) { param.append(item.getKey() + "=" + URLEncoder.encode(item.getValue()[0], "UTF-8") + "&"); logger.info(" " + item.getKey() + ": " + item.getValue()[0] + "&"); } } OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); // logger.info("Chamada: " + param.toString()); writer.write(param.toString()); writer.flush(); writer.close(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); param = null; param = new StringBuilder(); BufferedReader reader = new BufferedReader(in); String data; logger.info("Retorno da chamada: "); while ((data = reader.readLine()) != null) { param.append(data); } data = param.toString(); SetExpressCheckoutParser parser = SetExpressCheckoutParser.getInstance(); resp = parser.parse(data); // logger.debug("Resposta do SetExpressCheckout: " + resp.toString()); } catch (IOException ex) { logger.fatal("Erro ao executar SetExpressCheckout: " + ex.getLocalizedMessage(), ex); } return resp; }
// HTTP GET item from the server // Return 0 on success private Item2 getItem() { URL url; HttpsURLConnection urlConnection = null; Item2 item = null; String itemUrl = GET_ITEM_URL + "/" + mItem.getId(); try { url = new URL(itemUrl); urlConnection = (HttpsURLConnection) url.openConnection(); // Set authentication instance ID urlConnection.setRequestProperty( MyConstants.HTTP_HEADER_INSTANCE_ID, InstanceID.getInstance(getApplicationContext()).getId()); // Set content type urlConnection.setRequestProperty("Content-Type", "application/json"); // Set timeout urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); // Vernon debug Log.d(LOG_TAG, urlConnection.getRequestMethod() + " " + urlConnection.getURL().toString()); // Send and get response // getResponseCode() will automatically trigger connect() int responseCode = urlConnection.getResponseCode(); String responseMsg = urlConnection.getResponseMessage(); Log.d(LOG_TAG, "Response " + responseCode + " " + responseMsg); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { Log.d(LOG_TAG, "Server says the item was closed"); status = UpdateItemStatus.ITEM_CLOSED; return null; } if (responseCode != HttpURLConnection.HTTP_OK) { Log.d(LOG_TAG, "Get item " + mItem.getId() + " failed"); status = UpdateItemStatus.SERVER_FAILURE; return null; } // Get items from body InputStreamReader in = new InputStreamReader(urlConnection.getInputStream()); item = new Gson().fromJson(in, Item2.class); } catch (Exception e) { e.printStackTrace(); Log.d(LOG_TAG, "Get item failed because " + e.getMessage()); status = UpdateItemStatus.ANDROID_FAILURE; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return item; }
// HTTP PUT change to the server // Return 0 on success // Return 1 on Android failure // Return 2 on server failure private void updateItem() { URL url; HttpsURLConnection urlConnection = null; int size; byte[] data; OutputStream out; String itemUrl = UPDATE_ITEM_URL + "/" + mItem.getId(); try { url = new URL(itemUrl); urlConnection = (HttpsURLConnection) url.openConnection(); // Set authentication instance ID urlConnection.setRequestProperty( MyConstants.HTTP_HEADER_INSTANCE_ID, InstanceID.getInstance(getApplicationContext()).getId()); // Set content type urlConnection.setRequestProperty("Content-Type", "application/json"); // To upload data to a web server, configure the connection for output using // setDoOutput(true). It will use POST if setDoOutput(true) has been called. urlConnection.setDoOutput(true); urlConnection.setRequestMethod("PUT"); // Convert item to JSON string JSONObject jsonItem = new JSONObject(); JSONObject jsonMember = new JSONObject(); JSONArray jsonMembers = new JSONArray(); jsonMember.put("attendant", change); if (change >= 1 && mPhoneNumber != null && !mPhoneNumber.isEmpty()) { jsonMember.put("phonenumber", mPhoneNumber); } jsonMembers.put(jsonMember); jsonItem.put("members", jsonMembers); data = jsonItem.toString().getBytes(); // For best performance, you should call either setFixedLengthStreamingMode(int) when the // body length is known in advance, or setChunkedStreamingMode(int) when it is not. // Otherwise HttpURLConnection will be forced to buffer the complete request body in memory // before it is transmitted, wasting (and possibly exhausting) heap and increasing latency. size = data.length; if (size > 0) { urlConnection.setFixedLengthStreamingMode(size); } else { // Set default chunk size urlConnection.setChunkedStreamingMode(0); } // Get the OutputStream of HTTP client out = new BufferedOutputStream(urlConnection.getOutputStream()); // Copy from file to the HTTP client out.write(data); // Make sure to close streams, otherwise "unexpected end of stream" error will happen out.close(); // Check canceled if (isCancelled()) { Log.d(LOG_TAG, "Updating item canceled"); status = UpdateItemStatus.ANDROID_FAILURE; return; } // Set timeout urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); // Vernon debug Log.d( LOG_TAG, urlConnection.getRequestMethod() + " " + urlConnection.getURL().toString() + new String(data)); // Send and get response // getResponseCode() will automatically trigger connect() int responseCode = urlConnection.getResponseCode(); String responseMsg = urlConnection.getResponseMessage(); Log.d(LOG_TAG, "Response " + responseCode + " " + responseMsg); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { Log.d(LOG_TAG, "Server says the item was closed"); status = UpdateItemStatus.ITEM_CLOSED; return; } if (responseCode != HttpURLConnection.HTTP_OK) { Log.d(LOG_TAG, "Update item attendant " + change + " failed"); status = UpdateItemStatus.SERVER_FAILURE; return; } // Vernon debug Log.d(LOG_TAG, "Update item attendant " + change + " successfully"); } catch (Exception e) { e.printStackTrace(); Log.d(LOG_TAG, "Update item failed because " + e.getMessage()); status = UpdateItemStatus.ANDROID_FAILURE; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }