* 我有疑問,是否有可能從android中的服務器解析多個json響應?現在我正在開發一個連接android應用程序和cakephp網站的項目。我從服務器接收數據作爲json編碼格式。並在android中解析該json數據並顯示到視圖部分。 但現在我想從服務器傳遞多個json響應,怎麼可能? *如何解析來自服務器的Android中的多個Json數據
function commuterJson()
{
$upid=$_POST['upid'];
$ampm=$_POST['ampm'];
$this->loadModel('Userprofile');
$this->Userprofile->recursive = -1;
$ups = $this->Userprofile->find('first', array('conditions' =>
array('id' => $upid, 'status' => 'active')));
$todaysdata = $this->Requestcard->getRequestcardDataampm($upid, $ampm, $today);
$driverId=$todaysdata[0]['Requestcard']['driver_id'];
$vacencyId=$todaysdata[0]['Requestcard']['vacancycard_id'];
$driverDetails = $this->Userprofile->find('first', array('conditions' =>
array('id' => $driverId, 'status' => 'active')));
$vacancyDetails = $this->Vacancycard->find('first',
array('conditions' => array('id' =>$vacencyId)));
$vechicleId=$vacancyDetails['Vacancycard']['vehicledetail_id'];
$vechicleDetails=$this->Vehicledetail->find('first',
array('conditions' => array('id' => $vechicleId)));
echo json_encode($driverDetails);
echo json_encode($vechicleDetails);
echo json_encode($todaysdata);
exit();
}
我想,當我嘗試只有一個JSON數據傳遞到Android這三個JSO編碼的數據傳遞到Android
echo json_encode($driverDetails);
echo json_encode($vechicleDetails);
echo json_encode($todaysdata);
,它是正確越來越 我的Android代碼是
public void getData(View v)
{
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost =
new HttpPost("http://10.0.2.2/Mebuddie/logins/login1");
httppost.setEntity
(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader
(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
for (String line = null; (line = reader.readLine()) != null;)
{
builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);
Object type = new Object();
if (finalResult.length() == 0 && type.equals("both"))
{
System.out.println("null value in the json array");
}
else {
JSONObject json_data = new JSONObject();
for (int i = 0; i < finalResult.length(); i++)
{
json_data = finalResult.getJSONObject(i);
JSONObject menuObject = json_data.getJSONObject("Userprofile");
group_id= menuObject.getString("group_id");
id = menuObject.getString("id");
}
catch (Exception e) {
Toast.makeText(FirstMain.this,
"please enter a valid id or pswd",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
我需要在我的android代碼中添加接收多個json數據? 如果有人知道請回復.................
只是覺得......使它象:'{ 「driverDetails」:$ driverDetails, 「vechicleDetails」:$ vechicleDetails, 「todaysdata」:$ todaysdata}'或者像這樣不喜歡 – Selvin
嗨Selvin ...那是一個很有創意的想法..... Iam試圖這樣想.. –