如何解决 JSONException

How to solve JSONException

我正在尝试从 android 应用程序 activity 将数据连接到我的 SQL 数据库,但是每当我 运行 它时,我都会收到此错误:

W/System.err: org.json.JSONException: Valu of type java.lang.String cannot be converted to JSONObject

,如何解决这个问题?

这个寄存器class

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();
}
}

您的错误在这里:

JSONObject jObj = new JSONObject(response);

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

你的错误在这一行:

JSONObject jObj = new JSONObject(response);

试试下面的代码:

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

}

你用过排球吗?

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