Search Posts

Trying to send ArrayList to MySQL: Illegal String Offset

I am trying to send a list of users and book ids to my MySQL db using php.

I have the list of users and book ids in an ArrayList. I post them to them in an Object called watch. My current method of doing this worked fine with Strings but now I get a warning when I try send it to the server. I get an Illegal String Offset.

Android Client Code (Collecting the ArrayLists):

 Watch watch;
                    watch = new Watch(ClassesAdapter.getAttendees(), Lesson_ClassesAdapter.getLesson_IDs());

                    if(LoginActivity.sOnline){
                        sendWatch(watch);
                    }

Android Client (Sending “watch” to the database):

private void sendWatch(final Watch watch) {
    // Tag used to cancel the request
    String tag_string_req = "req_add_watch";
    // set the message of the dialog and shwo it in waiting the response
    mpDialog.setMessage(getResources().getString(R.string.Pdf_viewer_pDialog));
    showDialog();
    //send the message
    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_ADD_CLASS, new Response.Listener<String>() {


        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Main Response: " + response);
            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) {
                    MainActivity.sWatchs.add(watch);
                    Collections.sort(MainActivity.sWatchs, new Comparator<Watch>() {
                        @Override
                        public int compare(Watch tc1, Watch tc2) {
                            return tc1.getDate().compareTo(tc2.getDate());
                        }
                    });
                    hideDialog();
                    finish();
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                    hideDialog();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }



        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to url
            Map<String, String> params = new HashMap<>();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            String result =  gson.toJson(watch,Watch.class);
            params.put("watch",result);
            hideDialog();
            return params;
        }
    };
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

How I receive it in php:

if(isset($_POST['watch'])){
$jsonwatch = array($_POST['watch']);
$array = json_encode($jsonwatch, true);
$response["error"] =! ($db_watch->storeClass($array['attendees'], $array['lesson_iDs']));
}

Source: Stackoverflow

Leave a Reply

Your email address will not be published. Required fields are marked *