Created
May 31, 2017 14:05
-
-
Save matheusleite/1e1508f90d17694e54f843be1e1868c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void saveUserWithPhoto(final User user ) { | |
String url; | |
if( user.getId() > 0 ){ | |
url = ApiReference.User.getUpdateUrl(user.getId()); | |
} else { | |
url = ApiReference.User.getCreateUrl(); | |
} | |
CustomVolleyMultipartRequest multipartRequest = new CustomVolleyMultipartRequest(url, new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
handleSaveUserResponse(response); | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if( mOnSaveListener != null ) { | |
mOnSaveListener.onUserSaveFailure(error); | |
} | |
} | |
}){ | |
@Override | |
protected Map<String, String> getParams() { | |
Map<String, String> params = new HashMap<>(); | |
params.put("firstname", user.getFirstName()); | |
params.put("lastname", user.getLastName()); | |
params.put("email", user.getEmail()); | |
if( user.getPassword() != null ) { | |
params.put("password", user.getPassword()); | |
} | |
if( user.getId() > 0 ) { | |
SharedPreferences preferences = mContext.getSharedPreferences(SharedPreferencesReference.SHARED_PREFERENCES_IDENTIFIER, Context.MODE_PRIVATE); | |
String token = preferences.getString(SharedPreferencesReference.Auth.TOKEN_IDENTIFIER, ""); | |
params.put("token", token); | |
} | |
params.put( "pushDeviceToken", getDeviceToken() ); | |
return params; | |
} | |
@Override | |
protected Map<String, DataPart> getFiles() { | |
Map<String, DataPart> params = new HashMap<>(); | |
try { | |
Bitmap image = BitmapFromUri.getCorrectlyOrientedImageFromFilePath(user.getImage()); | |
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | |
image.compress(Bitmap.CompressFormat.JPEG, 100, stream); | |
byte[] byteArray = stream.toByteArray(); | |
params.put("photo", new DataPart(byteArray, ContentType.create("image/jpeg"), "photo.jpg") ); | |
image.recycle(); | |
} catch (IOException e) { | |
System.out.print(e.getMessage()); | |
} | |
return params; | |
} | |
}; | |
multipartRequest.setRetryPolicy(new DefaultRetryPolicy( | |
120000, | |
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, | |
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT | |
)); | |
multipartRequest.setTag(CREATE_USER_TAG); | |
mRequestQueue.add(multipartRequest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment