I want to send the userid and image name and a image to the server. i use MVVM and also Volley . but when i check my StringRequest , the image String is not sent in full. i use Base64 and … but i didnt get an answer.please help me Thank you
findViewById(R.id.buttonSubmitimg1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
try {
img1 = MediaStore.Images.Media.getBitmap(contentResolver, imageUriFront);
} catch (IOException e) {
throw new RuntimeException(e);
}
String uid="20";
userDocument.setUid(uid);
Log.d("SubmitCompleteInfo", "RequestBody: " +(imgtostring(img1)));
userDocument.setFirstImagePath(imgtostring(img1));
userDocument.setFirstImageName("first_image_" + uid + ".jpg");
informationUserViewModel.sendDocumentImg1(userDocument);
informationUserViewModel.getResponseLiveData().observe(DocumentInfo.this, response -> {
try {
JSONObject jsonResponse = new JSONObject(response);
String status = jsonResponse.getString("status");
if (status.equals("ok")) {
Toast.makeText(DocumentInfo.this, "اطلاعات ثبت شد.", Toast.LENGTH_SHORT).show();
} else {
// دریافت پیام خطا از پاسخ
String errorMessage = jsonResponse.optString("message", "خطا در ثبت اطلاعات");
Toast.makeText(DocumentInfo.this, errorMessage, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Toast.makeText(DocumentInfo.this, "خطا در پردازش پاسخ: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
private String imgtostring(Bitmap bitmap) {
int maxWidth = 800;
int maxHeight = 800;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleRatio = Math.min((float) maxWidth / width, (float) maxHeight / height);
int scaledWidth = Math.round(width * scaleRatio);
int scaledHeight = Math.round(height * scaleRatio);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
byte[] imageByte = outputStream.toByteArray();
String encodeImage = Base64.encodeToString(imageByte, Base64.DEFAULT);
return encodeImage;
}
and i send my request to server by volley
public void senDocumentInfoimg1(InformationUser.UserDocument userDocument, ResponseListener listener) {
try {
long timestamp = System.currentTimeMillis() / 1000;
String modifiedTimestamp = String.valueOf(timestamp).substring(0, String.valueOf(timestamp).length() – 2);
// Combine modified timestamp with token
String combinedToken = TOKEN + modifiedTimestamp;
Log.d("SubmitCompleteInfo", "RequestBody: " + combinedToken);
// تبدیل JSON به string
JSONObject jsonObject = new JSONObject();
jsonObject.put("uid", userDocument.getUid());
jsonObject.put("first_image_name", userDocument.getFirstImageName());
jsonObject.put("first_image_data", userDocument.getSecondImagePath());
final String requestBody = jsonObject.toString();
Log.d("SubmitCompleteInfo", "RequestBody: " + requestBody);
// فشردهسازی دادهها
StringRequest request = new StringRequest(
Request.Method.POST,
URL_SendDocument,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("SubmitCompleteInfo", "Response: " + response);
listener.onResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("SubmitCompleteInfo", "Volley Error: " + error.toString());
Log.e("SubmitCompleteInfo", "Network Response: " + error.networkResponse);
if (error.networkResponse != null) {
Log.e("SubmitCompleteInfo", "Status Code: " + error.networkResponse.statusCode);
try {
String responseBody = new String(error.networkResponse.data, "utf-8");
Log.e("SubmitCompleteInfo", "Response Body: " + responseBody);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
listener.onResponse(error.toString());
}
}
) {
@Nullable
@Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
return null;
}
}
@Override
public String getBodyContentType() {
return "application/octet-stream"; // تغییر Content-Type به binary
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("X-Authorization", combinedToken);
headers.put("Content-Encoding", "gzip"); // افزودن هدر برای فشردهسازی
return headers;
}
};
int customTimeoutMs = 20000;
request.setRetryPolicy(new DefaultRetryPolicy(customTimeoutMs,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance(context).addToRequestQueue(request);
} catch (JSONException e) {
Log.e("SubmitCompleteInfo", "JSON Error: " + e.toString()); // لاگ کردن خطای JSON
listener.onResponse(e.toString());
}
}
it is i send to server
{“uid”:”20″,”first_image_name”:”first_image_20.jpg”, “first_image_data”: here is the String that is not complete .