首页 > 解决方案 > 如果值为空,如何将(空格)作为值传递给 api

问题描述

我有一个更新配置文件 API,其中名字、姓氏是参数之一。我已将名称分成两个字符串,以空格作为分隔符。但是如果用户没有给出姓氏和更新,页面会崩溃,说“ArrayIndex Out of Bounds Exception”。我尝试使用 if 条件将姓氏值作为“空格”传递,以防姓氏为空。但它不起作用。请帮助验证此条件并相应地传递值。附上下面的具体代码:

代码

private void updateprofile() {
            firstname = edtName.getText().toString();
            lastname = edtName.getText().toString();
            splitstring = txtName.split(" ");
            firstname = splitstring[0].trim();
            lastname = splitstring[1].trim();

            if(TextUtils.isEmpty(lastname))
            {
                lastname=" ";
            }
    else
            {
                lastname = splitstring[1].trim();

            }
            Call<UpdateProfile> call = apiService.updateprofile(userId, firstname, lastname, profileurl, location, email, mobilenumber);

            Log.e("DATA PASSED", userId + " " + firstname + " " + lastname + " " + profileurl + " " + location + email + mobilenumber);
            call.enqueue(new Callback<UpdateProfile>() {
                @Override
                public void onResponse(Call<UpdateProfile> call, Response<UpdateProfile> response) {
                    if (response.isSuccessful()) {
                        String status = response.body().getStatus();
                        if (status.equalsIgnoreCase("1")) {

                            //Toast.makeText(getContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                            mainActivity.imgHomeMenu.setImageResource(R.drawable.edit_icon);
                            flagoption = true;
                            imgEditPhoto.setVisibility(View.GONE);
                            mainActivity.txvTitle.setText("PROFILE");
                            edtName.setEnabled(false);
                            edtLocation.setEnabled(false);
                            edtEmail.setEnabled(false);
                            edtPhone.setEnabled(false);
                            linearEmail.setBackground(getContext().getResources().getDrawable(R.drawable.button_background_profile_changes_two));

                        } else {
                            Toast.makeText(getContext(), response.body().getMessage(), Toast.LENGTH_SHORT).show();

                        }
                    }
                }

                @Override
                public void onFailure(Call<UpdateProfile> call, Throwable t) {

                }
            });
        } 

配置文件视图:

private void callProfileApi(Context context) {
        vehicleList = new ArrayList<>();

        Call<ProfileDetails> call = apiService.callProfile(userId);
        Log.e("USER ID INSIDE API", userId);
        call.enqueue(new Callback<ProfileDetails>() {
            @Override
            public void onResponse(Call<ProfileDetails> call, Response<ProfileDetails> response) {

                if (response.isSuccessful()) {
                    ProfileDetails resp = response.body();
                    if (resp != null) {
                        String status = resp.getStatus();

                        if (status.equalsIgnoreCase("1")) {

                            profileurl = resp.getUserDetail().getImage();
                            txtName = resp.getUserDetail().getName();
                            String joineddate = resp.getUserDetail().getJoined();
                            String[] join = joineddate.split(" ");
                            Log.e("join", join[0]);
                            splitstring = txtName.split(" ");
                            firstname = splitstring[0].trim();
                            lastname = splitstring[1].trim();
/*
                            if(edtName.getText().toString().trim().contains(" ")){
                                splitstring = txtName.split(" ");
                                firstname = splitstring[0].trim();
                                lastname = splitstring[1].trim();
                            }else{
                                lastname=" ";
                                Log.e("Name Error","Enter Name");
                            }*/
                            //txtjoinedDate = GlobalMethods.Date(join[0]);
                            txtjoinedDate = resp.getUserDetail().getJoined();
                            rating = resp.getUserDetail().getRating();
                            location = resp.getUserDetail().getLocation();
                            email = resp.getUserDetail().getEmail();
                            mobilenumber = resp.getUserDetail().getMobile();
                            ratingbar.setRating(Float.parseFloat(rating));

                            emailverification = resp.getUserDetail().getEmailVerification();
                            if (emailverification.equalsIgnoreCase("0")) {
                                txtEmail.setText("Verify");
                                txtEmail.setBackground(getResources().getDrawable(R.drawable.button_background_profile_changes_three));
                            } else {
                                txtEmail.setText("Verified");
                                txtEmail.setBackground(getResources().getDrawable(R.drawable.button_background_profile_changes_two));

                            }
                            mobileverfication = resp.getUserDetail().getMobileVerification();
                            if (mobileverfication.equalsIgnoreCase("0")) {
                                txtPhone.setText("Verify");
                                txtPhone.setBackground(getResources().getDrawable(R.drawable.button_background_profile_changes_three));
                            } else {
                                txtPhone.setText("Verified");
                                txtPhone.setBackground(getResources().getDrawable(R.drawable.button_background_profile_changes_two));

                            }

                            vehicleList = resp.getVehicles();
                            if (vehicleList.size() > 0) {
                                myVehicleAdapter = new MyVehicleProfileAdapter(getActivity(), vehicleList, "3");
                                recycleVehicleRegister.setAdapter(myVehicleAdapter);
                                recycleVehicleRegister.setLayoutManager(new GridLayoutManager(getActivity(), 1, LinearLayoutManager.HORIZONTAL, false));


                            } else {
                                recycleVehicleRegister.setVisibility(View.GONE);
                                txtNovehicles.setVisibility(View.VISIBLE);
                                vehiclelayout.setVisibility(View.GONE);

                            }
                            reviewList = resp.getReviews();
                            if (reviewList.size() > 0) {
                                profileReviewsAdapter = new ProfileReviewsAdapter(getContext(), reviewList);
                                recycleViewfeedback.setAdapter(profileReviewsAdapter);
                                recycleViewfeedback.setLayoutManager(new LinearLayoutManager(getContext()));
                            } else {
                                recycleViewfeedback.setVisibility(View.GONE);
                                txtNoreviews.setVisibility(View.VISIBLE);
                                morereviewslayout.setVisibility(View.GONE);
                                /*morereviewslayout.setVisibility(View.GONE);
                                recycleViewfeedback.setVisibility(View.GONE);
                                notfoundlayout.setVisibility(View.GONE);*/
                            }


                            if (TextUtils.isEmpty(profileurl)) {
                                userProfiPlaceholder.setImageDrawable(getContext().getResources().getDrawable(R.drawable.profile_placeholder));
                                Glide.with(getContext()).load(profileurl).into(userProfiPlaceholder);
                                edtName.setText(txtName);
                                Log.e("PROFILE NAME", txtName);


                                ratingbar.setRating(Float.parseFloat(rating));
                                txtRateText.setText(rating);
                                txtBalance.setText("$"+resp.getUserDetail().getReferralBalance());
                                edtLocation.setText(location);
                                edtEmail.setText(email);
                                edtPhone.setText(mobilenumber);
                                edtId.setText(idproof);
                            } else {

                                Glide.with(getContext()).load(profileurl).into(userProfiPlaceholder);
                                edtName.setText(txtName);
                                Log.e("PROFILE NAME", txtName);
                                txtJoinedDate.setText("Joined" + " " + txtjoinedDate);

                                ratingbar.setRating(Float.parseFloat(rating));
                                txtRateText.setText(rating);
                                txtBalance.setText("$"+resp.getUserDetail().getReferralBalance());
                                edtLocation.setText(location);
                                edtEmail.setText(email);
                                edtPhone.setText(mobilenumber);
                                edtId.setText(idproof);
                            }

                            if (TextUtils.isEmpty(mobilenumber)) {
                                edtPhone.setHint("Phone Number");
                            }
                            if (TextUtils.isEmpty(location)) {
                                edtLocation.setHint("Location");
                            }
                            if (TextUtils.isEmpty(idimage)) {
                                edtId.setHint("ID Verified (passport)");
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<ProfileDetails> call, Throwable t) {

            }
        });


    }

标签: androidretrofit

解决方案


如果您的字符串没有姓氏,那么即使在您检查空值之前,此语句也会导致错误。

lastname = splitstring[1].trim();

尝试直接检查size()您的数组是否大于您正在访问的索引。如果大小较大,则对姓氏进行初始化。


推荐阅读