android - 如果值为空,如何将(空格)作为值传递给 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) {
}
});
}
解决方案
如果您的字符串没有姓氏,那么即使在您检查空值之前,此语句也会导致错误。
lastname = splitstring[1].trim();
尝试直接检查size()
您的数组是否大于您正在访问的索引。如果大小较大,则对姓氏进行初始化。
推荐阅读
- javascript - TransitionGroup 中的 Reactjs CSSTransition 组件道具
- macos - lldb 在 macOS 11.1 上尝试“运行”arm64 二进制文件时给出“附加失败”
- python - 查找两个字符串的相对匹配
- javascript - 仅从异步操作返回的数组中获取第一个对象
- python - 图的连通分量算法
- javascript - 我的计算器按钮似乎没有连接到我的 javascript。控制台没有给我任何错误
- javascript - 如何减去这两个数组
- html - 如何用另一个属性的值替换一个属性值
- python - Python-Flask 无反应:活动一天后
- azure - 在 Azure 门户中,应用服务的模式是什么?