0

I'm still new at this and I need help. Maybe I have missed something...

Here's my PHP Code

<?php
    $con = mysqli_connect("localhost", "root", "", "customer");

    $order_name = $_POST["order_name"];
    $order_cust = $_POST["order_cust"];
    $quantity = $_POST["quantity"];

    $statement = mysqli_prepare($con, "INSERT INTO order (order_name, order_cust, quantity) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sss", $order_name, $order_cust, $quantity);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);
?>

and then here's my response code..

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;


public class OrderRequest extends StringRequest {

    private static final String ORDER_REQUEST_URL = "http://10.0.2.2/customer/Insert.php";
    private Map<String, String> params;

    public OrderRequest(String order_name, String order_cust, String quantity, Response.Listener<String> listener){
        super(Method.POST, ORDER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put ("order_name", order_name);
        params.put ("order_cust", order_cust);
        params.put ("quantity", quantity);
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

lastly this is my button activity code...

bConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String user_id = etUsername.getText().toString();
                final String date = etDate.getText().toString();
                final String time = etTime.getText().toString();
                final String pax = etPax.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){
                                AlertDialog.Builder builder = new AlertDialog.Builder(ReservationActivity.this);
                                builder.setMessage("Success! Your reservation has been added to our Queue!").setNegativeButton("Confirm", null).create().show();
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(ReservationActivity.this);
                                builder.setMessage("Reservation failed or missing credentials...").setNegativeButton("Retry", null).create().show();
                            }

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

                ReservationRequest reservationRequest = new ReservationRequest(user_id, date, time, pax, responseListener);
                RequestQueue queue = Volley.newRequestQueue(ReservationActivity.this);
                queue.add(reservationRequest);
            }
        });

Here's the LogCat

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

I need help pleaseee...

1
  • It seems your PHP code returned <br instead of some JSON. Why did it do that? Commented Sep 26, 2016 at 0:10

1 Answer 1

0

THe url you're requesting isn't sending JSON, its sending down html. So you can't parse it as JSON. Either your endpoint is wrong, it doesn't send JSON at all, or it sends HTML on errors. Or you have server bugs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.