0

In my android app, I want to insert data from database using php script. Php script is there , data should be successfully fetched from database and insert into database but in android side , getting error value array(2) of type java.lang.String .

function.php

 public function StoreListInfo($list_name,$list_title)
{
    $stmt = $this->conn->prepare("INSERT INTO ibeSaveList(list_name,list_title) VALUES(?,?)");
    $stmt->bind_param("ss", $list_name, $list_title);
    $result = $stmt->execute();
    $stmt->close();

    if($result)
    {
        $stmt = $this->conn->prepare("SELECT list_name,list_title FROM ibeSaveList WHERE list_title = ?");
        $stmt->bind_param("s",$list_title);
        $stmt->execute();
        $stmt->bind_result($token2,$token3);
        while( $stmt->fetch() )
        {
            $user["list_name"]=$token2;
            $user["list_title"]=$token3;
        }
        $stmt->close();
        return $user;
    }
    else
    {
        return false;
    }
}

StoreListInfo() which is called in web service.php

WebService.php
<?php
require_once 'update_user_info.php';
 $db = new update_user_info();
// json response array
$response = array("error" => FALSE);
var_dump($_POST);
if (isset($_POST['list_name']) && isset($_POST['list_title'])) {

 // receiving the post params
 $list_name = $_POST['list_name'];
 $list_title = $_POST['list_title'];

    // create a new user
    $user = $db->StoreListInfo($list_name,$list_title);
    if ($user) {
        // user stored successfully
        $response["error"] = FALSE;
        $response["user"]["list_name"] = $user["list_name"];
        $response["user"]["list_title"] = $user["list_title"];
        echo json_encode($response);
    } else {
        // user failed to store
        $response["error"] = TRUE;
        $response["error_msg"] = "Unknown error occurred in registration!";
        echo json_encode($response);
    }

 } else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (listname,listtitle) is missing!";
echo json_encode($response);
 }

 ?>

list.java

    private void createListUser(final String list_name, final String list_title) {
    // Tag used to cancel the request
      String cancel_req_tag = "createlist";

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

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

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

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

                if (!error) {
                    Intent intent = new Intent(getActivity(),
                            EventDetailActivity.class);
                    startActivity(intent);

                } else {

                    String errorMsg = jObj.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, "List 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("list_name", list_name);
            params.put("list_title", list_title);
            return params;
        }
    };
    // Adding request to request queue
    AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
}

In android output enter image description here

Developer_99
  • 105
  • 1
  • 5
  • 16

1 Answers1

0

The response that you are getting at android side doesn't seem to be valid. The response should just be this: {"error":false,"user":{"list_name":"test","list_title":"test"}}.

The response you are getting is this:

array(2) {
    ["list_name"] => 
    string(4) "test"
    ["list_title"] => 
    string(4) "test"
}
{"error":false,"user":{"list_name":"test","list_title":"test"}}

The response you are getting contains some more information just before the actual response as you can see in the logcat. That means you have some issue on the php side.

This should not be there:

array(2) {
    ["list_name"] => 
    string(4) "test"
    ["list_title"] => 
    string(4) "test"
}

Also you should set the content type header before doing echo

header('Content-Type: application/json');

Check this : Returning JSON from a PHP Script

miken32
  • 35,483
  • 13
  • 81
  • 108
Abhishek Jain
  • 3,261
  • 1
  • 23
  • 41