HttpGet mit Anfragekörper Android
Ich versuche, eine Anfrage auf dem Server von HttpGet zu machen. Aber im Nachrichtentext sollte ein json-Objekt stehen. Der folgende Code funktioniert nicht, da unit_id und sercret_key nicht in der Nachricht auf dem Server gesendet werden. Wie kann ich es tun?
JSONObject:
{
"unit_id": 12345,
"secret_key": "sdfadfsa6as987654754"
}
Mein Code:
private HttpResponse makeRequest(int id, String secretKey) throws Exception {
BasicHttpParams params = new BasicHttpParams();
params.setParameter("id", id);
params.setParameter("secret_key", secretKey);
httpget.setHeader("Accept", "application/json");
httpget.setParams(params);
httpget.setHeader("Content-type", "application/json");
// Handles what is returned from the page
return httpclient.execute(httpget);
}
Bearbeiten: in php wird diese Anfrage so gestellt
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://0101.apiary.io/api/reservations/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"unit_id\": 12345,\n \"secret_key\": \"sdfadfsa6as987654754\"\n}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);