My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Java: table of type java.lang.String cannot be converted to JSONObject

Itay Meir's photo
Itay Meir
·Jun 13, 2016

I have this code but whenever I run it I'm getting this error:

W/System.err: org.json.JSONException: Value < br>< table of type java.lang.String cannot be converted to JSONObject

CODE:

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ActionMenuItemView;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    final EditText etFirstName = (EditText) findViewById(R.id.etFirstName);
    final EditText etLastName = (EditText) findViewById(R.id.etLastName);
    final EditText etID = (EditText) findViewById(R.id.etID);
    final Button bRegister = (Button) findViewById(R.id.bRegister);

    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String firstname = etFirstName.getText().toString();
            final String lastname = etLastName.getText().toString();
            final String id = etID.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(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        }
                        else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(firstname, lastname, id, responseListener);
            RequestQueue queue =     Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}

PHP FILE:

<?php
$con = mysqli_connect("myhost", "myuser", "mypassword", "mysql");

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$id = $_POST["id"];

$statement = mysqli_prepare($con, "INSERT INTO users (firstname, lastname, id) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $firstname, $lastname, $id);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

LOGCAT:

06-13 11:01:39.874 2476-2476/ E/libprocessgroup: failed to make and chown         /acct/uid_10058: Read-only file system
06-13 11:01:39.875 2476-2476/ W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
06-13 11:01:39.875 2476-2476/ I/art: Not late-enabling -Xcheck:jni (already on)
06-13 11:01:44.646 2476-2489/ W/art: Suspending all threads took: 5.516ms
06-13 11:01:44.760 2476-2476/ W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-13 11:01:44.831 2476-2971/ D/OpenGLRenderer: Use     EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                               [ 06-13         11:01:44.833  2476: 2476 D/         ]
                                                                   HostConnection::get() New Host Connection established 0xb42e9f10, tid 2476
06-13 11:01:44.834 2476-2476/ D/Atlas: Validating map...
06-13 11:01:44.874 2476-2971/ I/OpenGLRenderer: Initialized EGL, version 1.4
06-13 11:01:44.910 2476-2971/ D/OpenGLRenderer: Enabling debug mode 0
06-13 11:01:44.921 2476-2971/ W/EGL_emulation: eglSurfaceAttrib not implemented
06-13 11:01:44.922 2476-2971/ W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xb42b84a0, error=EGL_SUCCESS
06-13 11:07:48.079 2476-2971/ W/EGL_emulation: eglSurfaceAttrib not implemented
06-13 11:07:48.079 2476-2971/ W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa393e520, error=EGL_SUCCESS
06-13 11:08:29.276 2476-2476/ W/System.err: org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject
06-13 11:08:29.276 2476-2476/ W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
06-13 11:08:29.276 2476-2476/ W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
06-13 11:08:29.276 2476-2476/ W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
06-13 11:08:29.276 2476-2476/ W/System.err:     at .RegisterActivity$1$1.onResponse(RegisterActivity.java:43)
06-13 11:08:29.276 2476-2476/ W/System.err:     at .RegisterActivity$1$1.onResponse(RegisterActivity.java:39)
06-13 11:08:29.276 2476-2476/ W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
06-13 11:08:29.276 2476-2476/ W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
06-13 11:08:29.276 2476-2476/ W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
06-13 11:08:29.276 2476-2476/ W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
06-13 11:08:29.276 2476-2476/ W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
06-13 11:08:29.276 2476-2476/ W/System.err:     at android.os.Looper.loop(Looper.java:135)
06-13 11:08:29.277 2476-2476/ W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
06-13 11:08:29.277 2476-2476/ W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
06-13 11:08:29.277 2476-2476/ W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
06-13 11:08:29.277 2476-2476/ W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-13 11:08:29.277 2476-2476/ W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Thanks in advance!