关于使用Retrofit2.0 向服务器上传图片的异常

时间:2022-08-28 19:27:06

private void uploadPricture() {

        if (uid > 0) {
            RequestBody userUid = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(uid));
            Map<String, RequestBody> map = new HashMap<>();
            map.put("uid", userUid);
            if (bitmap != null) {
                RequestBody fileBody = null;
                File tempfile = null;
                try {
                    tempfile = saveFile(bitmap);
                    fileBody = RequestBody.create(MediaType.parse("image/jpg"), tempfile);
                    map.put("image\"; filename=\"" + tempfile.getName(), fileBody);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                final File finalTempfile = tempfile;
                UserSettingModel.uploadPricture(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {

                        try {
                        String resultStr = response.body().string();
                            JSONObject jsonObj = new JSONObject(resultStr);
                            String iconImg = jsonObj.getString("iconImg");
                            String result = jsonObj.getString("result");
                            if (result.equals("0200")) {
                                //保存用户头像图片
                                LocalUserDataModel data = BaseUtils.readLocalUser(UserSettingActivity.this);
                                data.setUserImg(iconImg);
                                BaseUtils.saveLocalUser(UserSettingActivity.this, data);

                                userIcon.setImageURI(Uri.parse(iconImg));
                                Toast.makeText(UserSettingActivity.this, "上传头像成功", Toast.LENGTH_SHORT).show();
                                finalTempfile.delete();
                            } else {
                                Toast.makeText(UserSettingActivity.this, "上传头像失败", Toast.LENGTH_SHORT).show();
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            Log.d("@@@@@@@@@@@","上传头像失败" +e);
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(Throwable t) {
                        Toast.makeText(UserSettingActivity.this, "网络不给力 ", Toast.LENGTH_SHORT).show();
                    }
                }, map);
            }

        }

    }





 public static void uploadPricture(Callback<ResponseBody> callback, Map<String, RequestBody> data) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        RetrofitHttp.getRetrofit(builder.build()).uploadPicture("uploadPicture", data)
                .enqueue(callback);
    }




运行后选择图片确认后在

try {
                            String resultStr = response.body().string();
                           JSONObject jsonObj = new JSONObject(resultStr);
                            String iconImg = jsonObj.getString("iconImg");
                            String result = jsonObj.getString("result");
                            if (result.equals("0200")) {
                                //保存用户头像图片


这句话报异常 org.json.JSONException: Value 260 of type java.lang.Integer cannot be converted to JSONObjec

我打印 resultStr  的值 显示是0404     
所以猜测应该是在UserSettingModel.uploadPricture(new Callback<ResponseBody>(){   } ,map)这个方法里面出问题的 但有看不出是哪个地方

1 个解决方案

#1


各位大牛  走过路过不要错过  看看呗  关于使用Retrofit2.0 向服务器上传图片的异常

#1


各位大牛  走过路过不要错过  看看呗  关于使用Retrofit2.0 向服务器上传图片的异常