2013-08-05 102 views
0

问题是这样的,如果我使用测试值(在php文件中)处理json并且应用程序从服务器接收结果json,但是当我使用json值从应用程序,我不能让生成的JSON接收发送json到服务器和recive 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; 
} 

服务器接收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)); 


?> 
+0

你是什么意思?由应用程序生成的JSONData不能由Webserver处理,对吧?你会得到哪些错误? –

回答

0

它可能已经回答How to decode JSON values in my Android Aplication?

但是,如果你想从应用程序解析JSON数据你可能需要使用该标志真正的Web服务器

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

如果从Web服务器到应用程序解析数据的问题,迫使它通过使用

json_encode($data, JSON_FORCE_OBJECT); 

是一个对象,如果要分析一个数组使用

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

请解释你想要的和你得到的错误,找到适合你的案例的适当解决方案。