According to http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ , part 6, I want to send user and password, and retrieve status of login; as same as description of part 6!
I'm using volley library, and override getParams function to set Keys Values. And sent as POST to php page.
This is snippet code via volley library:
url = "http://www.myurl.com/app/page.php";
TAG = "tagToCancelReq";
JsonObjectRequest postRequest = new JsonObjectRequest (Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(JsonObject response) {
// response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("user", "client1");
params.put("pass", "123");
return params;
}
};
queue.addToRequest(postRequest,TAG);
This is php code:
**<?php
/* Encode array to JSON string */
function encodearray($posts) {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
}
$response = array();
if (isset($_POST['p']) && isset($_POST['v'])) {
$response["success"] = 1;
foreach ($_POST as $key => $value)
{
$response[$key] = $value;
}
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = mysql_select_db("db") or die(mysql_error());
$strsql = "INSERT INTO `users` (";
$strsql .= implode(",", array_keys($response));
$strsql .= ") VALUES ('";
$strsql .= implode("','", array_values($response));
$strsql .= "')";
mysql_query($strsql) or die($db->error());
/* encode the JSON post from the array */
encodearray($response);
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>**
In this way, server response success = 0.
But when using httpPost and Asynctask like this, server retrieve request and response correctly. Code is:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php");
// Add your data
List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (5);
nameValuePairs.add(new BasicNameValuePair("user", "client1"));
nameValuePairs.add(new BasicNameValuePair("pass", "123"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("myapp", "works till here. 2");
try {
HttpResponse response = httpclient.execute(httppost);
Log.d("myapp", "response " + response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
What is wrong with first way?
Even when i use local web server(wamp server), and change URL to
http://10.0.2.2:80/adnroid_server/get_All.php";
and run app on emulator, in first way (volley), server doesn't response (no error too), and in second way(httPost) every thing is correct and response retrieved.