List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:com.abiquo.apiclient.RestClient.java
License:Apache License
public <T extends SingleResourceTransportDto> T put(final String uri, final String accept, final String contentType, final SingleResourceTransportDto body, final Class<T> returnClass) { try {/*from w ww . j a va 2 s . c om*/ RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)), json.write(body)); Request request = new Request.Builder().url(absolute(uri)) .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).put(requestBody).build(); return execute(request, returnClass); } catch (IOException ex) { throw Throwables.propagate(ex); } }
From source file:com.abiquo.apiclient.RestClient.java
License:Apache License
public <T extends SingleResourceTransportDto> T put(final String uri, final String accept, final String contentType, final SingleResourceTransportDto body, final TypeToken<T> returnType) { try {//from ww w.ja va 2 s . c o m RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)), json.write(body)); Request request = new Request.Builder().url(absolute(uri)) .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).put(requestBody).build(); return execute(request, returnType); } catch (IOException ex) { throw Throwables.propagate(ex); } }
From source file:com.abiquo.apiclient.RestClient.java
License:Apache License
public void put(final String uri, final String accept, final String contentType, final SingleResourceTransportDto body) { try {/*from w w w.ja va 2 s .c o m*/ String rawBody = json.write(body); RequestBody requestBody = RequestBody.create(MediaType.parse(withVersion(contentType)), rawBody); Request request = new Request.Builder().url(absolute(uri)) .addHeader(HttpHeaders.ACCEPT, withVersion(accept)).put(requestBody).build(); execute(request, (Class<?>) null); } catch (IOException ex) { throw Throwables.propagate(ex); } }
From source file:com.afrisoftech.funsoft.mobilepay.MobilePayAPI.java
public void sendPaymentRequest(String accessToken) { OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); String message = null;//from w w w .j a v a2 s . com JSONObject json = new JSONObject(); try { json.put("InitiatorName", "Charles Waweru"); json.put("SecurityCredential", this.encryptInitiatorPassword("cert.cer", "3414")); json.put("CommandID", "CustomerPayBillOnline"); json.put("Amount", "10"); json.put("PartyA", "254714433693"); json.put("PartyB", "881100"); json.put("Remarks", "Testing Rest API"); json.put("QueueTimeOutURL", ""); json.put("ResultURL", "https://192.162.85.226:17933/FunsoftWebServices/funsoft/InvoiceService/mpesasettlement"); json.put("Occasion", "1234567891"); message = json.toString(); System.out.println("This is the JSON String : " + message); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } RequestBody body = RequestBody.create(mediaType, message); Request request = new Request.Builder().url("https://sandbox.safaricom.co.ke/mpesa/b2c/v1/paymentrequest") .post(body).addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); try { System.out.println("Request Build Security Credentials : [" + this.encryptInitiatorPassword("cert.cer", "3414") + "]"); System.out.println("Request Build : [" + request.body().toString() + "]"); Response response = client.newCall(request).execute(); JSONObject myObject = null; try { myObject = new JSONObject(response.body().string()); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Payment Request Response : " + myObject.toString()); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.afrisoftech.funsoft.mobilepay.MobilePayAPI.java
public void simulateTransaction(String accessToken) { System.out.println("Access token to use : [" + accessToken + "]"); OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\"ShortCode\":\"881100\"," + " \"CommandID\":\"CustomerPayBillOnline\"," + " \"Amount\":\"1\"," + " \"Msisdn\":\"254714433693\"," + " \"BillRefNumber\":\"1234567890\" }"); Request request = new Request.Builder().url("https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate") .post(body).addHeader("authorization", "Bearer " + accessToken.trim()) .addHeader("content-type", "application/json").build(); try {// w w w .j a va 2s .co m Response response = client.newCall(request).execute(); JSONObject myObject = null; try { myObject = new JSONObject(response.body().string()); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Response for simulate transaction : [" + myObject.toString() + "]\n\n"); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.afrisoftech.funsoft.mobilepay.MobilePayAPI.java
public static boolean sendProcessRequest(String accessToken, String accountNo, String payerMobilePhone, String billedAmount, String shortCode) { boolean checkoutRequestStatus = true; OkHttpClient client = new OkHttpClient(); Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); dateFormat.setCalendar(calendar);/* ww w. j a v a 2s . c om*/ String timeStamp = dateFormat.format(calendar.getTime()); System.out.println("Timestamp : [" + dateFormat.format(calendar.getTime()) + "]"); System.out.println("Shortcode : [" + shortCode + "]"); String password = shortCode + com.afrisoftech.hospital.HospitalMain.passKey + timeStamp; // String password = shortCode + "48d34200abe6ebbcbc3bc644487c3651936d129f2274f6ee95" + timeStamp; // json.put("CallBackURL", "https://192.162.85.226:17933/FunsoftWebServices/funsoft/InvoiceService/mpesasettlement"); // String password = shortCode + "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919" + timeStamp; // for testing purposes String encodedPassword = Base64.toBase64String(password.getBytes()); System.out.println("Unencoded password : [" + password + "]"); System.out.println("Encoded password : [" + encodedPassword + "]"); MediaType mediaType = MediaType.parse("application/json"); String message = null; JSONObject json = new JSONObject(); try { json.put("BusinessShortCode", shortCode); json.put("Password", encodedPassword); json.put("Timestamp", timeStamp); json.put("TransactionType", "CustomerPayBillOnline"); json.put("Amount", billedAmount); json.put("PartyA", payerMobilePhone); json.put("PartyB", shortCode); json.put("PhoneNumber", payerMobilePhone); json.put("CallBackURL", com.afrisoftech.hospital.HospitalMain.callBackURL); // json.put("CallBackURL", "https://192.162.85.226:17933/FunsoftWebServices/funsoft/InvoiceService/mpesasettlement"); json.put("AccountReference", accountNo); json.put("TransactionDesc", "Settlement for client bill"); message = json.toString(); System.out.println("This is the JSON String : " + message); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } RequestBody body = RequestBody.create(mediaType, message); Request request = null; if (com.afrisoftech.hospital.HospitalMain.mobileTxTest) { request = new Request.Builder().url("https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest") // for sandbox test cases .post(body).addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); } else { request = new Request.Builder().url("https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest") .post(body).addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); } try { Response response = client.newCall(request).execute(); JSONObject myJsonObject = null; try { myJsonObject = new JSONObject(response.body().string()); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } if (myJsonObject.toString().contains("error")) { try { // checkoutRequestID = myJsonObject.getString("errorMessage"); checkoutRequestStatus = false; System.out.println("Checkout Request ID : [" + myJsonObject.getString("errorMessage") + "]"); javax.swing.JOptionPane.showMessageDialog(null, "Payment Request Error : " + myJsonObject.getString("errorMessage") + ". Try again."); } catch (JSONException ex) { ex.printStackTrace(); } } else if (myJsonObject.toString().contains("Success")) { try { checkoutRequestStatus = true; com.afrisoftech.hospital.GeneralBillingIntfr.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); com.afrisoftech.hospinventory.PatientsBillingIntfr.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); com.afrisoftech.accounting.InpatientDepositIntfr.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); com.afrisoftech.accounting.InpatientRecpIntfr.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); com.afrisoftech.hospital.HospitalMain.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); com.afrisoftech.accounting.GovBillPaymentsIntfr.checkoutRequestID = myJsonObject .getString("CheckoutRequestID"); System.out .println("Checout Request ID : [" + myJsonObject.getString("CheckoutRequestID") + "]"); } catch (JSONException ex) { ex.printStackTrace(); } } System.out.println("Response for Process Request : [" + myJsonObject.toString() + "]"); } catch (IOException ex) { ex.printStackTrace(); } return checkoutRequestStatus; }
From source file:com.afrisoftech.funsoft.mobilepay.MobilePayAPI.java
public static void sendProcessRequestStatus(String accessToken, String checkoutRequestID) { OkHttpClient client = new OkHttpClient(); Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); dateFormat.setCalendar(calendar);//from ww w . ja va 2 s. co m String timeStamp = dateFormat.format(calendar.getTime()); System.out.println("Timestamp : [" + dateFormat.format(calendar.getTime()) + "]"); String password = "174379" + "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919" + timeStamp; String encodedPassword = Base64.toBase64String(password.getBytes()); ; System.out.println("Unencoded password : [" + password + "]"); System.out.println("Encoded password : [" + encodedPassword + "]"); MediaType mediaType = MediaType.parse("application/json"); String message = null; JSONObject json = new JSONObject(); try { json.put("BusinessShortCode", "174379"); //json.put("Password", "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTYwMjE2MTY1NjI3"); json.put("Password", encodedPassword); //json.put("Timestamp", "20160216165627"); json.put("Timestamp", timeStamp); json.put("CheckoutRequestID", checkoutRequestID); message = json.toString(); System.out.println("This is the JSON String : " + message); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } RequestBody body = RequestBody.create(mediaType, message); Request request = new Request.Builder().url("https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query") .post(body).addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); try { Response response = client.newCall(request).execute(); JSONObject myObject = null; try { myObject = new JSONObject(response.body().string()); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Response for Process Request : [" + myObject.toString() + "]"); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.afrisoftech.funsoft.mobilepay.MobilePayAPI.java
public static boolean registerCallbackURL(String accessToken, String shortCode, String callBackURL, String validationURL) {/*from w w w .j a v a 2 s . c o m*/ boolean checkoutRequestStatus = true; OkHttpClient client = new OkHttpClient(); MediaType mediaType = MediaType.parse("application/json"); String message = null; JSONObject json = new JSONObject(); try { json.put("ShortCode", shortCode); json.put("ConfirmationURL", callBackURL); json.put("ValidationURL", callBackURL); json.put("ResponseType", "Success"); message = json.toString(); System.out.println("This is the JSON String : " + message); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } RequestBody body = RequestBody.create(mediaType, message); Request request = null; if (com.afrisoftech.hospital.HospitalMain.mobileTxTest) { request = new Request.Builder().url("https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl") // for sandbox test cases .post(body).addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); } else { request = new Request.Builder().url("https://api.safaricom.co.ke/mpesa/c2b/v1/registerurl").post(body) .addHeader("authorization", "Bearer " + accessToken) .addHeader("content-type", "application/json").build(); } try { Response response = client.newCall(request).execute(); JSONObject myJsonObject = null; try { myJsonObject = new JSONObject(response.body().string()); } catch (JSONException ex) { Logger.getLogger(MobilePayAPI.class.getName()).log(Level.SEVERE, null, ex); } if (myJsonObject.toString().contains("error")) { try { // checkoutRequestID = myJsonObject.getString("errorMessage"); checkoutRequestStatus = false; System.out.println("Checkout Request ID : [" + myJsonObject.getString("errorMessage") + "]"); javax.swing.JOptionPane.showMessageDialog(null, "Payment Request Error : " + myJsonObject.getString("errorMessage") + ". Try again."); } catch (JSONException ex) { ex.printStackTrace(); } } else if (myJsonObject.toString().contains("Success")) { try { checkoutRequestStatus = true; System.out .println("Checout Request ID : [" + myJsonObject.getString("CheckoutRequestID") + "]"); } catch (JSONException ex) { ex.printStackTrace(); } } System.out.println("Response for Process Request : [" + myJsonObject.toString() + "]"); } catch (IOException ex) { ex.printStackTrace(); } return checkoutRequestStatus; }
From source file:com.aix.city.comm.OkHttpStack.java
License:Open Source License
@SuppressWarnings("deprecation") private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Request.Method.DEPRECATED_GET_OR_POST: // Ensure backwards compatibility. Volley assumes a request with a null body is a GET. byte[] postBody = request.getPostBody(); if (postBody != null) { builder.post(RequestBody.create(MediaType.parse(request.getPostBodyContentType()), postBody)); }/*from w ww.j a v a 2 s. c o m*/ break; case Request.Method.GET: builder.get(); break; case Request.Method.DELETE: builder.delete(); break; case Request.Method.POST: builder.post(createRequestBody(request)); break; case Request.Method.PUT: builder.put(createRequestBody(request)); break; case Request.Method.HEAD: builder.head(); break; case Request.Method.OPTIONS: builder.method("OPTIONS", null); break; case Request.Method.TRACE: builder.method("TRACE", null); break; case Request.Method.PATCH: builder.patch(createRequestBody(request)); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.aix.city.comm.OkHttpStack.java
License:Open Source License
private static RequestBody createRequestBody(Request r) throws AuthFailureError { final byte[] body = r.getBody(); if (body == null) return null; return RequestBody.create(MediaType.parse(r.getBodyContentType()), body); }