Abdu Hawi
Abdu Hawi

Reputation: 89

Android Volley JsonObjectRequest Post prameter in Kotlin

I use Volley with Java not have any problem. put when convert to kotlin i found some problem with send parameter with request I try used custom request class extends from Request this is not fix my problem also i used JsonObjectRequest with hash map some mistake the parameter not send with request. also used JsonObjectRequest with JSONObject also same mistake is still after that i used Post Man API the side from API is fine not has any problem also when used StringRequest not has any problem

my first code with JsonObjectRequest is

val url = "http://10.0.2.2/machine_project/includeJSON/system_machine.php"
val ahOBJ = JSONObject()
ahOBJ.put("dd", 2)

Log.d("TAG","kotJson")
val queu = Volley.newRequestQueue(this)
val ahReq = JsonObjectRequest(Request.Method.POST, url, ahOBJ, Response.Listener { response ->
    val str = response.toString()
    Log.d("TAG","response: $str")
}, Response.ErrorListener {
    error ->
    Log.d("TAG","response: ${error.message}")
})
queu.add(ahReq)

the second code is

val jr:RequestQueue = Volley.newRequestQueue(this)
            val params = HashMap<String,String>()
            params["dd"] = "2"
            Log.d("TAGTest", "Ready to go")
            val jsObj = JsonObjectRequest(Request.Method.POST,
                urlUP,
                JSONObject(params),
                Response.Listener
                {
                    response ->
                    Log.d("TAGTest", response.toString())
                },
                Response.ErrorListener {
                    error ->
                    Log.d("TAGTest", "error: ${error.message}")
                })
            jr.add(jsObj)

all result is

{"error":true,"msg":"All filed required"}

this result from back end API

the API is

$response = array();
if($_SERVER['REQUEST_METHOD'] == 'POST'){

    if( isset($_POST['dd'])){

        require_once ('systemMachineAPI.php');

        $result = get_sm();
        if($result != NULL){
            $response['error'] = false;
            $response['msg'] = $result;
        }else{
            $response['error'] = true;
            $response['msg'] = 'We Found Some Mistake';
        }
    }else{
        $response['error'] = true;
        $response['msg'] = 'All filed required';
    }

}else{
    $response['error'] = true;
    $response['msg'] = 'Cannot connect to server';
}

if any body can fix this or try use volley with kotlin post prameter please help me

Upvotes: 0

Views: 6508

Answers (1)

Ale-pr
Ale-pr

Reputation: 11

I know that's an old post and I hope you already found your way out of this, but I'm posting my solution in case someone else gets here.

I think, on the php side, you missed to decode the posted string.

By doing this on Kotlin

val ahOBJ = JSONObject()

ahOBJ.put("dd", 2)

you are providing Volley a json-style object to be sent to the php script, so on the php side you'll get a json-style string to work on, in stead of POST parameters.

In the PHP side try

// getting the posted data and decoding it to a json object
$postedContent = json_decode(file_get_contents("php://input"));

// $postedContent should now contain your 'dd' property

if (property_exists($postedContent, 'dd'))
{
    // yes, we got our property 
    echo $postedContent->dd;
}

Upvotes: 1

Related Questions