0

the problem is this, if I use the test values(in php file) the json is processed and the app receives the resulting json from the server, but when I use the json values ​​from app i cant get the resulting json

(Java):

private class enviarcoord extends AsyncTask{

@Override


protected JSONArray doInBackground(JSONObject... params) {
// TODO Auto-generated method stub

JSONObject Jobj =params[0];
HttpClient httpclient = new DefaultHttpClient();
HttpResponse resposta;
String respostasv ="";
JSONArray jsonArr = new JSONArray();

try{

HttpPost post = new HttpPost("MEU SITE ONLINE");
post.setEntity(new StringEntity(Jobj.toString(), "UTF8"));
resposta = httpclient.execute(post);

Log.i("Http Response:",resposta .toString());
String temp = EntityUtils.toString(resposta.getEntity());
Log.i("tag", temp);

//bota tentar

StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("MEU SITE ONLINE");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ParseException.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
respostasv = builder.toString();


// tratar a resposta

jsonArr = new JSONArray(respostasv);



}

catch (ClientProtocolException e) {
e.printStackTrace();
Log.i("ClienteProt", "ClientProtocolException");

}

catch (IOException e) {
e.printStackTrace();
Log.i("IOE", "IOException");

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("IOE", "erro a criar o array json");
}

// se der erro o return vai devolver vazio
return jsonArr;
}

the server receives the json and do processing like this:

(PHP):

require_once "db/DB.php"; //connect to a database/disconnect handler.



$jobj = json_decode($HTTP_RAW_POST_DATA);

$earth_radius = 3960.00; # em milhas

$MlocLat_1 = $jobj->Latitude;
$MlocLon_1 = $jobj->Longitude;
$Distancia = $jobj->Distancia;

// VALORES DE TESTES
/*

$MlocLat_1 = "41.6529";
$MlocLon_1 = "-8.58453";
$Distancia =1;

*/


$conjcoord = 0;

$db = DB::getDB();
$sql = "SELECT * FROM coordenada";
$idterreno=0;

$result=$db->query($sql);


while($row=$result->fetch_object()){
$conjcoord++;

if(distance_haversine($MlocLat_1, $MlocLon_1, $row->Latitude, $row->Longitude) <= $Distancia*1000){


if($idterreno != $row->id_info){
$sql2 = "SELECT * FROM informacao WHERE Id_informacao= $row->id_info ";
$result2=$db->query($sql2);


while($row2=$result2->fetch_object()){
$output[]=$row2;
}
$idterreno =$row->id_info;
}

}


}


//########### calculo distancia entre pontos gps
function distance_haversine($lat1, $lon1, $lat2, $lon2) {
global $earth_radius;



$delta_lat = $lat2 - $lat1 ;
$delta_lon = $lon2 - $lon1 ;

$alpha = $delta_lat/2;
$beta = $delta_lon/2;
$a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
$c = asin(min(1, sqrt($a)));
$distance = 2*$earth_radius * $c;
$distance = round($distance, 4);
$distance = $distance * 1.609344;
return $distance;
}


print (json_encode($output));


?>
tshepang
  • 12,111
  • 21
  • 91
  • 136
Rick45
  • 1
  • 1
  • What do you mean? The JSONData generated by the app cant be processed by the Webserver, right? Which errors do you get? – Emanuel Jun 15 '14 at 11:49

1 Answers1

0

It may be already answered in How to decode JSON values in my Android Aplication?

BUT,

if you want to parse JSON Data from the App to the Webserver you may want to use the flag true.

$post = json_decode(filter_input(INPUT_POST, 'data'), true);
foreach ($post as $obj) {
  echo  $obj["yourobj"];
}

If you have problems parsing the data from the webserver to the app, force it to be an object by using

json_encode($data, JSON_FORCE_OBJECT); 

if you want to parse an array use

json_encode(array("data", $yourdata)); 

Please explain what you want and the error you get for a proper solution matching your case.

Community
  • 1
  • 1
Emanuel
  • 8,027
  • 2
  • 37
  • 56