0

I got problem to pass the value inside the variable of Android Studio to the php code. In this case I want to pass the value inside the variable "group_id" in Message.java to the DbOperation.php. That means at the end, the function "getMessages" inside the DbOperation.php can get the value of variable "group_id" and select the particular table inside the MySQL database. I still new to Android Studio and please help me to solve this problem. Tq very much.

For example: the value of variable "group_id" is "ABC123", the "getMessages" function inside DbOperation.php will perform "SELECT a.id, a.message, a.sentat, a.users_id, b.name FROM ABC123_messages a, users b WHERE a.users_id = b.id ORDER BY a.id ASC;"

This code below is the java class of the Message.java

 SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Config.SHARED_PREF_GROUP, Context.MODE_PRIVATE);
 group_id = sharedPreferences.getString(Config.GROUP_SHARED_PREF, "Not Available");


 private void fetchMessages() {
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_FETCH_MESSAGES,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //dialog.dismiss();

                    try {
                        JSONObject res = new JSONObject(response);
                        JSONArray thread = res.getJSONArray("messages");
                        for (int i = 0; i < thread.length(); i++) {
                            JSONObject obj = thread.getJSONObject(i);
                            int userId = obj.getInt("userid");
                            String message = obj.getString("message");
                            String name = obj.getString("name");
                            String sentAt = obj.getString("sentat");
                            Message messagObject = new Message(userId, message, sentAt, name);
                            messages.add(messagObject);
                        }

                        adapter = new ThreadAdapter(getActivity(), messages, AppController.getInstance().getUserId());
                        recyclerView.setAdapter(adapter);
                        scrollToBottom();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("group_id", group_id);
            return params;
        }
    };

    AppController.getInstance().addToRequestQueue(stringRequest);
}

This is part of php code in index.php

$app->post('/messages', function () use ($app){
verifyRequiredParams(array('group_id'));
$group_id = $app->request()->post('group_id');
$db = new DbOperation();
$messages = $db->getMessages($group_id);
$response = array();
$response['error']=false;
$response['messages'] = array();
while($row = mysqli_fetch_array($messages)){
    $temp = array();
    $temp['id']=$row['id'];
    $temp['message']=$row['message'];
    $temp['userid']=$row['users_id'];
    $temp['sentat']=$row['sentat'];
    $temp['name']=$row['name'];
    array_push($response['messages'],$temp);
}
echoResponse(200,$response);});


function verifyRequiredParams($required_fields){
$error = false;
$error_fields = "";
$request_params = $_REQUEST;
// Handling PUT request params
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $app = \Slim\Slim::getInstance();
    parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
    if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
        $error = true;
        $error_fields .= $field . ', ';
    }
}

if ($error) {
    // Required field(s) are missing or empty
    // echo error json and stop the app
    $response = array();
    $app = \Slim\Slim::getInstance();
    $response["error"] = true;
    $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
    echoResponse(400, $response);
    $app->stop();
}}

This is one of the function inside DbOperation.php

public function getMessages($group_id){
    $stmt = $this->conn->prepare("SELECT a.id, a.message, a.sentat, a.users_id, b.name FROM ?_messages a, users b WHERE a.users_id = b.id ORDER BY a.id ASC;");
    $stmt->bind_param("s",$group_id);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result;}
ping94
  • 67
  • 1
  • 1
  • 7

1 Answers1

0

In Android code looks fine. But I think you haven't caught the send data in PHP. Using

 $_POST["group_id"]
M--
  • 25,431
  • 8
  • 61
  • 93