Pranshu K
Pranshu K

Reputation: 73

Data is not passing to server through volley

I'm trying to send data to the server to check login by using volley but data is not being received at the server end. I'm getting an error message

Notice: Undefined index: username in C:\xampp2\htdocs\serverconnect.php on line 5

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        StringRequest stringRequest  = new StringRequest(Request.Method.GET, "http://192.168.43.144/mr_loc_tracker.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                    Log.i("Server Response",response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    Log.i("Server Error Response",error.toString());
                }
            }
        ){

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


                return params;
            }
        };
        MySingleton.getInstance(MainActivity.this).addToRequestQueue(stringRequest);
    }
}

Code of MySingleton.java:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue requestQueue;
    private  static Context mCtx;

    private MySingleton(Context context){
        mCtx = context;
        requestQueue = getRequestQueue();
    }

    public static synchronized MySingleton getInstance(Context context){
        if (mInstance==null)
        {
            mInstance= new MySingleton(context);
        }
        return mInstance;
    }

    public <T>void addToRequestQueue(Request<T> request){
        requestQueue.add(request);
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue==null)
        {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());

        }
        return requestQueue;
    }
}

Code of serverconnect.php

 <?php

 $username = $_GET["username"];

 echo $username;

 ?>

I'm getting the error

Notice: Undefined index: username in C:\xampp2\htdocs\serverconnect.php on line 5

Upvotes: 0

Views: 68

Answers (3)

Archu Mohan
Archu Mohan

Reputation: 219

<?php
 require 'config.php'; // your databsee configuration file
 $username = $_GET['username'];

$sql = "SELECT * FROM users(user table) where email='$email'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>0)
{
    $status = "exist";
}
echo json_encode(array("response"=>$status));
mysqli_close($con);

 ?>

Upvotes: 0

P.Juni
P.Juni

Reputation: 2485

Just change $_GET -> $_POST and Request.Method.GET -> Request.Method.POST

If you want to stay with GET method remove the getParams() function, you have to pass param in your url ex.: http://192.168.43.144/mr_loc_tracker.php?username=doe

Upvotes: 1

Archu Mohan
Archu Mohan

Reputation: 219

Upload your (php) server connection codes and database names and all

Upvotes: 0

Related Questions