0

问题是这样的,如果我使用测试值(在php文件中)处理json并且应用程序从服务器接收生成的json,但是当我使用来自应用程序的json值时我无法得到生成的json

(爪哇):

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;
}

服务器接收 json 并进行如下处理:

(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));


?>
4

1 回答 1

0

如何在我的 Android 应用程序中解码 JSON 值?

但,

如果您想将 JSON 数据从应用程序解析到 Web 服务器,您可能需要使用标志 true。

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

如果您在将数据从网络服务器解析到应用程序时遇到问题,请使用以下命令将其强制为对象

json_encode($data, JSON_FORCE_OBJECT); 

如果你想解析一个数组使用

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

请解释您想要什么以及您遇到与您的案例相匹配的正确解决方案的错误。

于 2014-06-15T11:52:16.493 回答