List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:com.forateq.cloudcheetah.fragments.UpdateHREmployeeFragment.java
@OnClick(R.id.ripple_update) public void update() { if (isSave) { if (isNetworkAvailable()) { final ProgressDialog mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.setIndeterminate(true); mProgressDialog.setMessage("Updating Employee..."); mProgressDialog.show();/* w w w. j a v a 2s .c o m*/ final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(ApplicationContext.get()); String sessionKey = sharedPreferences.getString(AccountGeneral.SESSION_KEY, ""); String userName = sharedPreferences.getString(AccountGeneral.ACCOUNT_USERNAME, ""); String deviceid = Settings.Secure.getString(ApplicationContext.get().getContentResolver(), Settings.Secure.ANDROID_ID); if (isPicChange) { MultipartBuilder multipartBuilder = new MultipartBuilder(); File file = new File(profileImageFileNameTV.getText().toString()); RequestBody image = RequestBody.create(MediaType.parse("image/jpg"), file); multipartBuilder.addFormDataPart("image", file.getName(), image); fileRequestBody = multipartBuilder.build(); } else { MultipartBuilder multipartBuilder = new MultipartBuilder(); multipartBuilder.addFormDataPart("image", ""); fileRequestBody = multipartBuilder.build(); } Observable<EmployeeResponseWrapper> observable = cloudCheetahAPIService.updateEmployee(employee_id, firstNameET.getText().toString(), middleNameET.getText().toString(), lastNameET.getText().toString(), genderSP.getSelectedItemPosition(), birthDayET.getText().toString(), addressET.getText().toString(), emailET.getText().toString(), contactNoET.getText().toString(), 1, titleET.getText().toString(), employmentTypeSP.getSelectedItemPosition(), zipCodeET.getText().toString(), tinNoET.getText().toString(), sssNoET.getText().toString(), driverLicenseNoET.getText().toString(), civilStatusSP.getSelectedItemPosition(), notesET.getText().toString(), fileRequestBody, userName, deviceid, sessionKey, AccountGeneral.METHOD_PUT); observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .unsubscribeOn(Schedulers.io()).subscribe(new Subscriber<EmployeeResponseWrapper>() { @Override public void onCompleted() { if (mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } updateSaveIV.setImageResource(R.mipmap.ic_mode_edit_white_24dp); isSave = false; disAbleUpdate(); MainActivity.popFragment(); } @Override public void onError(Throwable e) { if (mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } Log.e("UpdateEmployee", e.getMessage(), e); } @Override public void onNext(EmployeeResponseWrapper employeeResponseWrapper) { if (employeeResponseWrapper.getResponse() .getStatus_code() == AccountGeneral.SUCCESS_CODE) { Log.e("Name", employeeResponseWrapper.getData().getFirst_name()); employees.setFirst_name(firstNameET.getText().toString()); employees.setMiddle_name(middleNameET.getText().toString()); employees.setLast_name(lastNameET.getText().toString()); employees.setDate_of_birth(birthDayET.getText().toString()); employees.setGender_id(genderSP.getSelectedItemPosition()); employees.setAddress(addressET.getText().toString()); employees.setEmail_address(emailET.getText().toString()); employees.setContact_no(contactNoET.getText().toString()); employees.setTitle(titleET.getText().toString()); employees.setEmployment_type_id(employmentTypeSP.getSelectedItemPosition()); employees.setZip_code(zipCodeET.getText().toString()); employees.setTin_no(tinNoET.getText().toString()); employees.setSss_no(sssNoET.getText().toString()); employees.setDrivers_license_no(driverLicenseNoET.getText().toString()); employees.setCivil_status_id(civilStatusSP.getSelectedItemPosition()); employees.setNotes(notesET.getText().toString()); employees.setImage(employeeResponseWrapper.getData().getImage()); employees.save(); } else { Toast.makeText(getActivity(), "There is a problem updating the employee please try again later.", Toast.LENGTH_SHORT).show(); } } }); } else { Toast.makeText(ApplicationContext.get(), "Please connect to a network to update the item.", Toast.LENGTH_SHORT).show(); } } else { updateSaveIV.setImageResource(R.mipmap.ic_save_white_24dp); isSave = true; enableUpdate(); } }
From source file:com.frostwire.http.HttpClient.java
License:Open Source License
private RequestBody buildReqBody(Request request) { if (request.mime() != null && request.body() != null) { return RequestBody.create(MediaType.parse(request.mime()), request.body()); } else {/*from ww w . j a v a 2s . c om*/ return null; } }
From source file:com.frostwire.util.http.OKHTTPClient.java
License:Open Source License
private String post(String url, int timeout, String userAgent, String postContentType, byte[] postData, boolean gzip) throws IOException { canceled = false;//from w ww . ja va2 s . co m final OkHttpClient okHttpClient = newOkHttpClient(); final Request.Builder builder = prepareRequestBuilder(okHttpClient, url, timeout, userAgent, null, null); final RequestBody requestBody = RequestBody.create(MediaType.parse(postContentType), postData); prepareOkHttpClientForPost(okHttpClient, gzip); builder.post(requestBody); return getPostSyncResponse(builder); }
From source file:com.gezhii.fitgroup.network.OkHttpStack.java
License:Open Source License
private static RequestBody createRequestBody(Request r) throws AuthFailureError { byte[] body = r.getBody(); // if (body == null) return null; if (body == null) { body = new byte[0]; }//from www.ja v a 2s.co m return RequestBody.create(MediaType.parse(r.getBodyContentType()), body); }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject getAutomatelyCloudPlan() { try {// www . j a v a 2s . c o m JsonObject requestData = new JsonObject(); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder() .url(USER_API_ENDPOINT + "/getPlan").post(RequestBody .create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject setAutomatelyCloudPlan(String planId) { try {//from w w w .j a v a2 s . co m JsonObject requestData = new JsonObject(); requestData.putString("planId", planId); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder() .url(USER_API_ENDPOINT + "/changePlan").post(RequestBody .create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.automately.sdk.Automately.java
License:Mozilla Public License
public static JsonObject registerModule(JsonObject manifest) { try {//from www .j av a 2 s. com JsonObject requestData = new JsonObject(); // The module register requires authentication requestData.putObject("manifest", manifest); requestData.putString("username", getApiUsername()); requestData.putString("apiKey", getApiKey()); Request request = new Request.Builder().url(REGISTRY_ENDPOINT + "/submit").post( RequestBody.create(MediaType.parse("application/json"), requestData.toString().getBytes())) .build(); Response response = httpClient.newCall(request).execute(); if (response != null) { String responseEntity = response.body().string(); try { checkAuthorized(responseEntity); return new JsonObject(responseEntity); } catch (DecodeException ignored) { getFormattedError("Decode Exception", "There was an issue decoding the response."); } } } catch (IOException ignored) { } return null; }
From source file:com.github.kskelm.baringo.ImageService.java
License:Open Source License
/** * Upload an image to Imgur by pointing at a Url on the internet. * Must be available openly without authentication. * <p>// w w w. ja v a 2 s . c o m * <b>ACCESS: ANONYMOUS</b> or <b>AUTHENTICATED USER</b> * @param Url the full URL of the image. * @param fileName original of the file being uploaded (pick something) * @param albumId the name of the album, the album's deleteHash if it's anonymous, or null if none * @param title title of image or null if none * @param description description of image or null if none * @return The new Image object. If this is anonymous, <i>hang on to the delete hash</i> or you won't be able to manipulate it in the future! * @throws BaringoApiException something terrible happened to Stuart! */ public Image uploadUrlImage(String Url, String fileName, String albumId, String title, String description) throws BaringoApiException { RequestBody body = RequestBody.create(MediaType.parse("text/plain"), Url); Call<ImgurResponseWrapper<Image>> call = client.getApi().uploadUrlImage(albumId, "URL", title, description, body); try { Response<ImgurResponseWrapper<Image>> res = call.execute(); ImgurResponseWrapper<Image> out = res.body(); client.throwOnWrapperError(res); return out.getData(); } catch (IOException e) { throw new BaringoApiException(e.getMessage()); } // try-catch }
From source file:com.github.kskelm.baringo.ImageService.java
License:Open Source License
/** * Upload an image to Imgur as a stream from the local filesystem. * Use a buffered stream wherever possible! * <p>//from www . ja v a 2s . c om * <b>ACCESS: ANONYMOUS</b> or <b>AUTHENTICATED USER</b> * @param mimeType mime type like image/png. If null, Baringo will try to infer this from the fileName. * @param fileName name of the file being uploaded * @param albumId the name of the album, the album's deleteHash if it's anonymous, or null if none * @param title title of image or null if none * @param description description of image or null if none * @return The new Image object. If this is anonymous, <i>hang on to the delete hash</i> or you won't be able to manipulate it in the future! * @throws IOException Something was wrong with the file or streaming didn't work * @throws BaringoApiException que sera sera */ public Image uploadLocalImage(String mimeType, String fileName, String albumId, String title, String description) throws IOException, BaringoApiException { // can be null File file = new File(fileName); if (!file.exists()) { throw new FileNotFoundException("File not found: " + fileName); } // if if (!file.canRead()) { throw new IOException("Cannot access file " + fileName); } // if if (mimeType == null) { // infer from file prefix int dotAt = fileName.lastIndexOf('.'); if (dotAt == -1) { throw new BaringoApiException("Could not infer mime type" + " from file name; no extension"); } // if String ext = fileName.substring(dotAt + 1).toLowerCase(); mimeType = extensionToMimeType.get(ext); if (mimeType == null) { throw new BaringoApiException("Could not infer mime type" + " from extension '" + ext + "'"); } // if } // if // strip the directory hierarchy off the filename. Path path = Paths.get(fileName); fileName = path.getFileName().toString(); RequestBody body = RequestBody.create(MediaType.parse(mimeType), file); Call<ImgurResponseWrapper<Image>> call = client.getApi().uploadLocalImage(albumId, "file", title, description, fileName, body); try { Response<ImgurResponseWrapper<Image>> res = call.execute(); ImgurResponseWrapper<Image> out = res.body(); client.throwOnWrapperError(res); return out.getData(); } catch (IOException e) { throw new BaringoApiException(e.getMessage()); } // try-catch }
From source file:com.github.leonardoxh.temporeal.model.listeners.PushSender.java
License:Apache License
public static void send(String entity) { Request.Builder requestBuilder = new Request.Builder(); requestBuilder.url(ApplicationConfiguration.PUSH_SERVICE_URL); requestBuilder// w w w. j a v a 2 s. c o m .post(RequestBody.create(MediaType.parse("application/json"), getJsonOfRegistrationsId(entity))); requestBuilder.header("Authorization", ApplicationConfiguration.GCM_PROJECT_ID); OkHttpClient okHttp = new OkHttpClient(); try { Response response = okHttp.newCall(requestBuilder.build()).execute(); System.out.println("Response code: " + response.code()); } catch (IOException e) { e.printStackTrace(); } }