Problème d'envoi d'objets au serveur avec gson
Salut tout le monde ça fait des heures que je cherche ou est l'erreur : j'envois un objet java en format json avec la librairie Gson à un script php mais le script n'a pas l'air de recevoir les données ... voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
public static String syncAddTaskOnline(TaskAddress addr, long onlinePIID){
String donnees = "l'objet addr est null";
if (addr == null){
return donnees;
}
else{
String result = null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String jsonAdd = gson.toJson(addr);
System.out.println(jsonAdd);
nameValuePairs.add(new BasicNameValuePair("addr", jsonAdd));
nameValuePairs.add(new BasicNameValuePair("onlinePIID", String.valueOf(onlinePIID)));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.13/spotnshare/syncAddressPIOnline.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
... |
mon System.out.println(jsonAdd); affiche ==> System.out(27192): {"address":"Avenue BLABLA 130,Woaint-Laert,1200,Belgique","postCode":0,"longitude":3.44838,"id":0,"latitude":40.8462,"number":0}
il est donc bien sous le format json avant d'etre envoyé au serveur, niveau serveur j'ai :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<?php
/* On verif qu'on a bien recu les 3 données */
if( isset($_POST['addr']) && isset($_POST['onlinePIID'])) {
// on se connecte à notre base pour recuperer les data
include("connexion_bdd.php");
if(connexionBDD() == 1){
$TaskAddress = $_POST['addr'];
$jAddress = json_decode($TaskAddress);
echo(json_encode(array("aaaaa" => $_POST['addr'])));
$address = $jAddress->address;
$lat = $jAddress->latitude;
$long = $jAddress->longitude; |
j'ai placé un echo car je voyais a chaque fois cette erreur apparaitre : " Notice: Trying to get property of non-object in C:\wamp\www\spotnshare\syncAddressPIOnline.php on line <i>16</i>" ligne 17 et 18 aussi donc pour ==>
$address = $jAddress->address;
$lat = $jAddress->latitude;
$long = $jAddress->longitude;
et du coup dans mon echo je vois que mon objet est null dans mon logcat ==> [{"aaaaa":null] ...mais ce que je comprend vrmt pas c'est que j'ai utilisé exactement le meme code que pour un autre objet ou ca a parfaitement fonctionné :s ... ==>
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static long syncTaskOnline(TaskReminder task){
String result = null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String jsonTask = gson.toJson(task);
nameValuePairs.add(new BasicNameValuePair("taskReminder", jsonTask));
long donnees = 0;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.13/spotnshare/syncTaskOnline.php");
... |
et la partie php de ce code qui fonctionne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
<?php
<?php
/* On verif qu'on a bien recu les 3 données */
if( isset($_POST['taskReminder'])) {
// on se connecte à notre base pour recuperer les data
include("connexion_bdd.php");
if(connexionBDD() == 1){
$TaskReminder = $_POST['taskReminder'];
$jTaskReminder = json_decode($TaskReminder);
$id = $jTaskReminder->id;
$localID = $jTaskReminder->localID;
$userID = $jTaskReminder->userID;
$title = $jTaskReminder->title;
$text = $jTaskReminder->text;
$dateTime = 0;
$dateTimeUp = 0;
... |
J'ai comparé mes codes je ne vois vrmt pas ou est l'erreur :S