Reputation: 77
i want to send a request with basic auth.
My header
Username : EBA Token : 34242353453456563DSFS
And my gsonRequestClass like this :
public GsonRequest(int method, String url, Class<T> responseClass,
Map<String, String> parameters, Map<String, String> headers, Listener<T> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
this.mResponseClass = responseClass;
this.mParameters = parameters;
this.mHeaders = headers;
}
/**
* Header getter method
*
* @return
* @throws AuthFailureError
*/
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return mHeaders != null ? mHeaders : super.getHeaders();
}
Any advice or sample code please ?
Upvotes: 0
Views: 2846
Reputation: 458
If you want to authenticate a
token
the api providers likeyahoo
,etc
etc
provides code samples for all of theserver-side languages like PHP, JAVA
, etc etc and you are not required to use volley.
But as you have asked question on volley and i have been developed so many android apps using volley, i am starting answer to the volley
:
I am assuming..
Here we go...
Java code to make a http request to a server page :
StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://www.yourserver.com/get_auth.php",
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
serverResponse = response;
// get response string here; if successful
Toast.makeText(getActivity(),response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
serverResponse = error.toString();
// If request could not be placed then error report here
Toast.makeText(getActivity(),error.toString(),Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String,String> getParams()
{
Map<String,String> params = new HashMap<String, String>();
params.put("Username",FirstLast);
params.put("Token",EmailField.getText().toString());
return params;
}};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
If you have success in importing library... above code will just need to auto insert library by placing cursor over below two lines and use OPT + RETURN on mac; which will import and include necessary packages for your above request.
On your web server get_auth.php should be like :
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$UserInput = $_POST['Username'];
$UserId = $_POST['Token'];
$con = mysqli_connect("localhost", "id4859061_instapromo", "sandhya12345","id4859061_instapromo");
if (!$con)
{
echo "EXCEPTION_CODE_1";
exit();
}
$result = $con->query("SELECT * FROM USERS WHERE `DEVID` = '$UserId' AND `KEYGEN` = '$UserInput'");
if (!$result)
{
echo "EXCEPTION_CODE_2";
exit();
}
if ($result->num_rows == 0)
{
echo "FAILURE";
exit();
}
if ($result->num_rows == 1)
{
$row = $result->fetch_assoc();
$TempThisDate = $row["VALUEDATE"];
$InstDate = DateTime::createFromFormat('d/m/Y',$TempThisDate);
$InstallDate = $InstDate->format('d-m-Y');
$Response = "SUCCESS#".$InstallDate;
echo $Response;
exit();
}
if ($result->num_rows > 1)
{
echo "REDUNDANT_RECORDS_FOUND";
exit();
}
}
else
{
echo "EXCEPTION_CODE_3";
}
In this way from web-server you have to echo / json your data back to requesting app
Java code already
toasts
this server response sent from PHP page. This is a basic working and step by step example.. I hope you or someone else find this useful.
Upvotes: 1