【问题标题】:How to solve JSONException如何解决 JSONException
【发布时间】:2018-04-06 15:17:40
【问题描述】:

我正在尝试从一个 android 应用程序活动中创建一个注册页面,将数据连接到我的 SQL 数据库,但每当我运行它时,我都会收到此错误:

W/System.err:org.json.JSONException:java.lang.String 类型的值 无法转换为 JSONObject

,这个问题怎么解决?

这个寄存器类

public class RegistrationActivity extends AppCompatActivity {

private static final String TAG = "RegisterActivity";
private static final String URL_FOR_REGISTRATION = "http://192.168.1.33/arswebservices/register.php";
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);
   final EditText NameRegister = (EditText) findViewById(R.id.reg_UserName);
    final  EditText EmailRegister = (EditText) findViewById(R.id.reg_Email);
    final EditText PasswordRegister = (EditText)                                           findViewById(R.id.reg_Password);
    Button registrationButton = (Button) findViewById(R.id.reg_Button);

    // Progress dialog
    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    registrationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            registerUser(NameRegister.getText().toString(), EmailRegister.getText().toString(),
                    PasswordRegister.getText().toString());
        }
    });
}

private void registerUser(final String name,  final String email, final String password) {
    // Tag used to cancel the request
    String cancel_req_tag = "register";

    progressDialog.setMessage("Adding you ...");
    showDialog();

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_FOR_REGISTRATION, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");


                if (!error) {
                    String user = jObj.getJSONObject("user").getString("name");
                    Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            RegistrationActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {

                    String errorMsg = jObj.getJSONObject("user").getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
    // Adding request to request queue
    AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
}

private void showDialog() {
    if (!progressDialog.isShowing())
        progressDialog.show();
}

private void hideDialog() {
    if (progressDialog.isShowing())
        progressDialog.dismiss();
}
}

【问题讨论】:

  • 请输入您的 JSON 字符串。
  • 你的意思是php文件??
  • 响应字符串
  • 您在哪一行收到此错误?

标签: php android android-studio android-database jsonexception


【解决方案1】:

你用过凌空吗?

请参阅下面的链接获取注册表,您可能会发现它很有用
https://www.android-examples.com/volley-user-registration/

【讨论】:

    【解决方案2】:

    你的错误在这里:

    JSONObject jObj = new JSONObject(response);
    

    确保您在响应中返回了一个 jsonObject !

    【讨论】:

      【解决方案3】:

      你的错误在这一行:

      JSONObject jObj = new JSONObject(response);
      

      试试下面的代码:

      try{
            JSONObject jObj = new JSONObject("{"+response+"}");
      }catch (JSONException e) {
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-02
        • 2011-06-22
        • 1970-01-01
        • 2013-04-24
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多