public class VinhNT_HTTP implements Response.Listener,Response.ErrorListener {
private VinhNT_Activity context;
private RequestQueue queue;
public VinhNT_HTTP(VinhNT_Activity nguCanh){
context = nguCanh;
// Instantiate the cache
Cache cache = new DiskBasedCache(context.getCacheDir(), 1024 * 1024); // 1MB cap
cache.clear();
//
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
//
// Instantiate the RequestQueue with the cache and network.
queue = new RequestQueue(cache,network);
//
}
public void sendRequest() {
//
queue.start();
try{
String url ="http://192.168.0.101/bongda";
JSONObject jsonBody = new JSONObject();
jsonBody.put("user","VinhNT");
jsonBody.put("function", "login");
JsonObjectRequest a = new JsonObjectRequest(Request.Method.POST,url,jsonBody,this,this);
queue.add(a);
//queue.stop();
}
/*catch (JSONException e){
Log.d("Error","JSONException");
}*/
catch (Exception e){
Log.d("Error","JSONException"+ e.getMessage());
}
}
public void cancelAll(){
queue.cancelAll(context.getTitle_VinhNT());
}
@Override
public void onResponse(JSONObject response) {
Log.d("Ket noi","OK roi nhe");
}
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Ket noi","Loi roi");
}
}
That doesn't send JSONObject to PHP server:
<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$request_body = file_get_contents('php://input');//lay body request
//$request_body = $_POST['json'];
$inputArray = json_decode($request_body,true);
//
//get function name
$function = $inputArray['user'];
//chose function
switch ($function){
case "login":
echo "{'status':'OK'}";
break;
default:
echo "{'status2':'" . count($inputArray) ."'}";
break;
}
//if($function == 'login')
//dau gi day
//$ga = new class_dir\BaseInput();
I'm trying with code:
public class VinhNT_HTTP implements Response.Listener,Response.ErrorListener {
private VinhNT_Activity context;
private RequestQueue queue;
public VinhNT_HTTP(VinhNT_Activity nguCanh){
context = nguCanh;
// Instantiate the cache
Cache cache = new DiskBasedCache(context.getCacheDir(), 1024 * 1024); // 1MB cap
cache.clear();
//
// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());
//
// Instantiate the RequestQueue with the cache and network.
queue = new RequestQueue(cache,network);
}
That doesn't send JSONObject to PHP server: <?php function __autoload($class_name) { include $class_name . '.php'; } $request_body = file_get_contents('php://input');//lay body request //$request_body = $_POST['json']; $inputArray = json_decode($request_body,true); // //get function name $function = $inputArray['user']; //chose function switch ($function){ case "login": echo "{'status':'OK'}"; break; default: echo "{'status2':'" . count($inputArray) ."'}"; break; } //if($function == 'login') //dau gi day //$ga = new class_dir\BaseInput();
?>
please help me, many thank