php - org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject (Andriod Studio) -
i'm trying android app developed in android studio send data register user activity database. idea once user types in required information , clicks register button data should go database , login activity should re-appear. however, when click on register button on register user activity nothing happening, no redirection login activity or data being sent database.
below register user activity code:
package com.example.vi5h.split; import android.content.intent; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import com.android.volley.requestqueue; import com.android.volley.response; import com.android.volley.toolbox.volley; import org.json.jsonexception; import org.json.jsonobject; public class registeruseractivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_register_user); final edittext etfullname = (edittext) findviewbyid(r.id.etfullname); final edittext etusername = (edittext) findviewbyid(r.id.etusername); final edittext etpassword = (edittext) findviewbyid(r.id.etpassword); final edittext etemail = (edittext) findviewbyid(r.id.etemail); final button btnregister = (button) findviewbyid(r.id.btnregister); btnregister.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final string name = etfullname.gettext().tostring(); final string username = etusername.gettext().tostring(); final string password = etpassword.gettext().tostring(); final string email = etemail.gettext().tostring(); response.listener<string> responselistener = new response.listener<string>(){ @override public void onresponse(string response) { try { jsonobject jsonresponse = new jsonobject(response); boolean success = jsonresponse.getboolean("success"); if (success){ intent intent = new intent(registeruseractivity.this, loginactivity.class); registeruseractivity.this.startactivity(intent); }else { alertdialog.builder builder = new alertdialog.builder(registeruseractivity.this); builder.setmessage("register failed") .setnegativebutton("retry", null) .create() .show(); } } catch (jsonexception e) { e.printstacktrace(); } } }; registerrequest registerrequest = new registerrequest(name, username, email, password, responselistener); requestqueue queue = volley.newrequestqueue(registeruseractivity.this); queue.add(registerrequest); } }); } }
below register request class code:
package com.example.vi5h.split; import com.android.volley.response; import com.android.volley.toolbox.stringrequest; import java.util.hashmap; import java.util.map; public class registerrequest extends stringrequest { private static final string register_request_url = "https://split.000webhostapp.com/register.php"; private map<string, string> params; public registerrequest(string name, string username, string email, string password, response.listener<string> listener) { super(method.post, register_request_url, listener, null); params = new hashmap<>(); params.put("name", name); params.put("username", username); params.put("email", email); params.put("password", password); } @override public map<string, string> getparams() { return params; } }
below php script (register.php):
<?php $con = mysqli_connect("localhost", "id2833909_split421", "pass321", "id2833909_splitw"); $name = $_post["name"]; $email = $_post["email"]; $username = $_post["username"]; $password = $_post["password"]; $statement = mysqli_prepare($con, "insert user (name, username, email, password) values (?, ?, ?, ?)"); mysqli_stmt_bind_param($statement, "siss", $name, $username, $email, $password); mysqli_stmt_execute($statement); $response = array(); $response["success"] = true; echo json_encode($response); ?>
and finnally below error thrown on logcat after tapped register button: https://imgur.com/b8x0su6
if visit https://split.000webhostapp.com/register.php in web browser, broken content. not complete web page, instead bunch of error messages:
<br /> <b>notice</b>: undefined index: name in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>4</b><br /> <br /> <b>notice</b>: undefined index: email in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>5</b><br /> <br /> <b>notice</b>: undefined index: username in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>6</b><br /> <br /> <b>notice</b>: undefined index: password in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>7</b><br /> <br /> <b>warning</b>: mysqli_stmt_bind_param() expects parameter 1 mysqli_stmt, boolean given in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>9</b><br /> <br /> <b>warning</b>: mysqli_stmt_execute() expects parameter 1 mysqli_stmt, boolean given in <b>/storage/ssd5/909/2833909/public_html/register.php</b> on line <b>10</b><br /> {"success":true}
the first 3 characters of response <br
. happen first 3 characters json parser complaining in error message. so, guess web server sending same sort of broken content in response volley request.
so, take @ response
, see getting plain text. then, fix whatever problem response
indicates.
Comments
Post a Comment