You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

注册表单API调用无响应求助:无报错无返回且自动退回上一页

注册表单API调用问题

我正在开发注册表单功能,调用API时遇到以下问题:

  • API既不返回成功消息,也不返回错误信息,无法定位代码问题
  • 点击提交按钮后页面直接退回上一页
  • 尝试通过Logcat打印日志也无输出

相关代码如下:

navigationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BusinessType.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "Please Specify the Business Type", Toast.LENGTH_LONG).show();
                }
                else if (AdhaarNumber.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Adhaar Number", Toast.LENGTH_LONG).show();
                }
                else if (PAN.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the PAN Number", Toast.LENGTH_LONG).show();
                }
                else if (GSTIN.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the GSTIN Number", Toast.LENGTH_LONG).show();
                }
                else if (CompanyName.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Company Name", Toast.LENGTH_LONG).show();
                }
                else if (Description.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Description about" +
                            "your Company ", Toast.LENGTH_LONG).show();
                }else {
                        Company.setVisibility(View.GONE);
                        Address.setVisibility(View.VISIBLE);
                        Contact.setVisibility(View.GONE);

                        position = 1;
                        stepView.done(false);
                        stepView.go(position,true);
                        navigationButton.setText("Next");
                }

                if (CompanyContactNumber.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "Please Specify the Contact " +
                            "Number of Company", Toast.LENGTH_LONG).show();
                }
                else if (CompanyEmail.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Company Email", Toast.LENGTH_LONG).show();
                }
                else if (Pincode.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Pincode", Toast.LENGTH_LONG).show();
                }
                else if (Country.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Country", Toast.LENGTH_LONG).show();
                }
                else if (State.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the State", Toast.LENGTH_LONG).show();
                }
                else if (City.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the City", Toast.LENGTH_LONG).show();
                }
                else if (Address1.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Address 1", Toast.LENGTH_LONG).show();
                }
                else if (Address2.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Address 2", Toast.LENGTH_LONG).show();
                } else {
                    Company.setVisibility(View.GONE);
                    Address.setVisibility(View.GONE);
                    Contact.setVisibility(View.VISIBLE);

                    position = 2;
                    stepView.done(false);
                    stepView.go(position,true);
                    navigationButton.setText("Submit");
                }

                if (Name.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "Please Specify the Name", Toast.LENGTH_LONG).show();
                }
                else if (Email.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Email", Toast.LENGTH_LONG).show();
                }
                else if (Mobile.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Mobile Number", Toast.LENGTH_LONG).show();
                }
                else if (Password.getText().toString().isEmpty()) {
                    Toast.makeText(Registration.this, "Please Specify the Password", Toast.LENGTH_LONG).show();
                }else {

                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("compId",0);
                        jsonObject.put("companyName",SelectedCompanyName);
                        jsonObject.put("compPrefix",null);
                        jsonObject.put("compPrefixPanAa",null);
                        jsonObject.put("companyType",3);
                        jsonObject.put("buisnessTypeId",businessTypeID);
                        jsonObject.put("description",SelectedDescription);
                        jsonObject.put("gstin",SelectedGSTIN);
                        jsonObject.put("aadharNumber",SelectedAdhaarNumber);
                        jsonObject.put("panNumber",SelectedPANNumebr);
                        jsonObject.put("associateCompanyId",41);
                        jsonObject.put("zoneId",0);
                        jsonObject.put("companyEmail",SelectedCompanyEmail);
                        jsonObject.put("companyContact",SelectedCompanyNumber);
                        jsonObject.put("companyLogo",null);
                        jsonObject.put("addressLine1",SelectedAddress1);
                        jsonObject.put("addressLine2",SelectedAddress2);
                        jsonObject.put("country",1);
                        jsonObject.put("city",160152);
                        jsonObject.put("state",56);
                        jsonObject.put("pinCode","121004");
                        jsonObject.put("contactPersonName",SelectedName);
                        jsonObject.put("contactEmail",SelectedEmail);
                        jsonObject.put("contactMobile",Integer.valueOf(SelectedMobileNumber));
                        jsonObject.put("userPassword",SelectedPassword);
                        jsonObject.put("tcsApplicable",false);
                        jsonObject.put("createByCompId",41);
                        jsonObject.put("isActive",true);
                        jsonObject.put("lastUpdatedBy",1);
                        Log.e("requestBody", jsonObject.toString());
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                    SubmitRegistration(jsonObject);
                }
            }
        });

        //Get BusinessType Data
        GetBusinessTypeData getBusinessTypeData = new GetBusinessTypeData();
        getBusinessTypeData.execute();
    }

    private void SubmitRegistration(JSONObject jsonObject) {
        Retrofit retrofit = BaseURL.getRetrofit();
        API apiServices = retrofit.create(API.class);
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), (jsonObject).toString());
        Call<JsonObject> call = apiServices.NewRegistration(requestBody);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                try {
                    JSONObject jsonObject = new JSONObject(new Gson().toJson(response.body()));
                    String responseCode = jsonObject.getString("responseCode");
                    String responseMessage = jsonObject.getString("responseMessage");
                    Log.e("response", jsonObject.toString());
                } catch (JSONException e) {
                    Log.e("response", e.getMessage());
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Log.e("response", t.getMessage());
            }
        });
    }

问题排查与修复建议

1. 按钮点击逻辑结构错误

当前代码中三个大的if-else块是并列执行的,无论当前处于注册流程的哪一步(公司信息/地址/联系人),三个块都会依次判断。比如在第一步填写完公司信息后,代码会切换到地址页,但紧接着会执行地址信息的空判断(此时地址栏为空),弹出Toast的同时还会执行联系人信息的判断,直接导致逻辑混乱,甚至触发异常崩溃。

修复方式:根据position变量区分当前步骤,只执行对应步骤的判断:

navigationButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch(position) {
            case 0: // 公司信息步骤
                if (BusinessType.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "请填写企业类型", Toast.LENGTH_LONG).show();
                }
                // 其余公司信息判断逻辑...
                else {
                    // 切换到地址页的逻辑
                    Company.setVisibility(View.GONE);
                    Address.setVisibility(View.VISIBLE);
                    Contact.setVisibility(View.GONE);
                    position = 1;
                    stepView.done(false);
                    stepView.go(position,true);
                    navigationButton.setText("Next");
                }
                break;
            case 1: // 地址信息步骤
                if (CompanyContactNumber.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "请填写企业联系电话", Toast.LENGTH_LONG).show();
                }
                // 其余地址信息判断逻辑...
                else {
                    // 切换到联系人页的逻辑
                    Company.setVisibility(View.GONE);
                    Address.setVisibility(View.GONE);
                    Contact.setVisibility(View.VISIBLE);
                    position = 2;
                    stepView.done(false);
                    stepView.go(position,true);
                    navigationButton.setText("Submit");
                }
                break;
            case 2: // 联系人信息提交步骤
                if (Name.getText().toString().isEmpty()){
                    Toast.makeText(Registration.this, "请填写姓名", Toast.LENGTH_LONG).show();
                }
                // 其余联系人信息判断逻辑...
                else {
                    // 构建JSON并调用API的逻辑
                    JSONObject jsonObject = new JSONObject();
                    try {
                        // 填充JSON数据...
                        Log.e("requestBody", jsonObject.toString());
                    } catch (JSONException e) {
                        Log.e("JSON_ERROR", e.getMessage());
                    }
                    SubmitRegistration(jsonObject);
                }
                break;
        }
    }
});

2. 日志无输出问题

  • 确认是否进入提交逻辑:在每个步骤的else块开头添加日志,比如Log.e("STEP", "进入地址页步骤"),判断代码是否执行到API调用环节。
  • 修复异常抛出导致的崩溃:去掉throw new RuntimeException(e);,异常会直接终止程序,导致后续日志无法打印,改为仅输出日志:
    catch (JSONException e) {
        Log.e("JSON_ERROR", e.getMessage());
    }
    
  • 完善Retrofit响应处理:先判断响应是否成功,避免空指针,同时打印错误响应体:
    @Override
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
        if(response.isSuccessful() && response.body() != null) {
            try {
                JSONObject jsonObject = new JSONObject(new Gson().toJson(response.body()));
                String responseCode = jsonObject.getString("responseCode");
                String responseMessage = jsonObject.getString("responseMessage");
                Log.e("response", jsonObject.toString());
            } catch (JSONException e) {
                Log.e("response_parse_error", e.getMessage());
            }
        } else {
            Log.e("response_error", "请求失败,状态码:" + response.code());
            // 打印错误响应体
            try {
                if(response.errorBody() != null) {
                    Log.e("error_body", response.errorBody().string());
                }
            } catch (IOException e) {
                Log.e("error_body_parse", e.getMessage());
            }
        }
    }
    
  • 检查基础配置:确认AndroidManifest.xml中添加了网络权限<uses-permission android:name="android.permission.INTERNET"/>,Retrofit的BaseURL配置正确。

3. 页面回退问题

页面回退大概率是逻辑混乱导致的异常崩溃,修复上述按钮点击逻辑的结构错误,同时去掉不必要的RuntimeException抛出,即可解决。另外检查代码中是否有其他地方调用了finish()或返回栈操作。


内容的提问来源于stack exchange,提问作者Bhoomika Vashistha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:44:54