0

我想使用 volley library android studio 将 base64 图像字符串发送到服务器。它在邮递员中工作,并给我完美的响应,如图所示,如图所示

我在 android studio 中尝试了很多次,但它总是向我显示 500 代码错误并且无法得到任何人帮助我的结果。查看我附在这篇文章中的图片中的邮递员响应。我还附上了我的代码。

这是图像按钮单击侦听器事件:

   profile_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, 22);
            }
        });

现在开始活动的结果代码在这里:

@Override
public void onActivityResult(int requestCode, final int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

     if (requestCode == 22 && resultCode == RESULT_OK && data != null){
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
            Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
            profile_image.setImageBitmap(selectedImage);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            selectedImage.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();
            String encoded4 = Base64.encodeToString(byteArray, Base64.DEFAULT);

            String encoded22 = "data:image/jpeg;base64,"+encoded4;
            Toast.makeText(getContext(), encoded22, Toast.LENGTH_SHORT).show();

                final ProgressDialog progress = new ProgressDialog(getContext());
                progress.setTitle("Uploading Image");
                progress.setMessage("Loading...");
                progress.setCancelable(false);
                progress.show();

                RequestQueue queue = Volley.newRequestQueue(getContext());


                final JSONObject jsonObject = new JSONObject();

                    jsonObject.put("base64image", encoded22);

                    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, Utils.baseUrl +"/company/update-photo", jsonObject,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {

                                    progress.dismiss();
                                    Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();

                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            progress.dismiss();
                            Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show();
                            error.printStackTrace();
                        }
                    }) {
                        @Override
                        public Map<String, String> getHeaders() {
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("Content-Type", "application/json");
                            params.put("Authorization", bear + idCheck);
                            return params;
                        }
                    };
                  jsonRequest.setRetryPolicy(new DefaultRetryPolicy(
                    120000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(jsonRequest);

                } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), e.toString(), Toast.LENGTH_SHORT).show();
        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getContext(), "Problem is here", Toast.LENGTH_SHORT).show();
        }

     }



}

但它总是向我显示这个错误

“ BasicNetwork.performRequest:URL 的意外响应代码 500”

4

0 回答 0