Click here to Skip to main content
14,450,167 members

Android volley post sending parameter is NULL

MAHESH WAGHELA asked:

Open original thread
I am trying to insert data into mysql and its shows "data inserted successfully" but blank row inserted in mysql and for that I have DEBUG and found that Volley stringRequest is Null. I think Parameter is NULL in such case how to deal please suggest.


my code is belwo

<pre><?php

include 'DatabaseConfig.php' ;
 
 $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
 
  $f_name = false;
 if(isset($_POST['name'])){
     $f_name = $_POST['name'];

}
echo $f_name;

$l_name = false;
if(isset($_POST['fname'])){
     $l_name = $_POST['fname'];

}
echo $l_name;

    
 
   
 
 $Sql_Query = "insert into dajs (name,fname) values ('$f_name','$l_name')";
 
 if(mysqli_query($con,$Sql_Query)){
 
 echo 'Data Inserted Successfully';
 
 }
 else{
 
 echo 'Try Again';
 
 }
 mysqli_close($con);
?>



and my java code is below

<pre>package com.example.digitalrukhivalmikisamaj;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import android.widget.Button;

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

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;



public class applicationform extends AppCompatActivity {

    TextView tv;

    // Creating EditText.
    EditText FirstName, LastName ;

    // Creating button;
    Button InsertButton;

    // Creating Volley RequestQueue.
    RequestQueue requestQueue;

    // Create string variable to hold the EditText Value.
    String FirstNameHolder, LastNameHolder;

    // Creating Progress dialog.
    ProgressDialog progressDialog;

    // Storing server url into String variable.
    String HttpUrl = "https://dajsapp.000webhostapp.com/get_data1.php";

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

        tv=findViewById(R.id.ttppl);
        tv.setText("ડિજિટલ ગુજરાતી અનુસૂચિત\n જાતિ સમાજ એપ ");

        // Assigning ID's to EditText.
        FirstName = (EditText) findViewById(R.id.name);
        LastName = (EditText) findViewById(R.id.fname);

        // Assigning ID's to Button.
        InsertButton = (Button) findViewById(R.id.btn1);

        // Creating Volley newRequestQueue .
        requestQueue = Volley.newRequestQueue(applicationform.this);

        progressDialog = new ProgressDialog(applicationform.this);

        // Adding click listener to button.
        InsertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Showing progress dialog at user registration time.
                progressDialog.setMessage("Please Wait, We are Inserting Your Data on Server");
                progressDialog.show();

                // Calling method to get value from EditText.
                GetValueFromEditText();



                // Creating string request with post method.
                StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String ServerResponse) {

                                // Hiding the progress dialog after all task complete.
                                progressDialog.dismiss();

                                // Showing response message coming from server.
                                Toast.makeText(applicationform.this, ServerResponse, Toast.LENGTH_LONG).show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {

                                // Hiding the progress dialog after all task complete.
                                progressDialog.dismiss();

                                // Showing error message if something goes wrong.
                                Toast.makeText(applicationform.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                            }
                        }) {
                    @Override
                    protected Map<String, String> getParams() {

                        // Creating Map String Params.
                        Map<String, String> params = new HashMap<String, String>();

                        // Adding All values to Params.
                        params.put("first_name", FirstNameHolder);
                        params.put("last_name", LastNameHolder);

                        return params;
                    }

                };


                // Creating RequestQueue.
                RequestQueue requestQueue = Volley.newRequestQueue(applicationform.this);

                // Adding the StringRequest object into requestQueue.
                requestQueue.add(stringRequest);

            }
        });


    }

    // Creating method to get value from EditText.
    public void GetValueFromEditText(){

        FirstNameHolder = FirstName.getText().toString().trim();
        LastNameHolder = LastName.getText().toString().trim();
        


    }




}



So finally from DEBUG Its a Volley stringRequest Parameter send NULL Please suggest how to overcome from this issue.

What I have tried:

I have searched many blog but not find any perfect idea hence I am here for seeking help.
Tags: Java, Android, PHP

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100