*我有一个疑问,是否可以在 android 中解析来自服务器的多个 json 响应?现在我正在开发一个将 android 应用程序与 cakephp 网站连接起来的项目。我以 json 编码格式从服务器接收数据。并在 android 中解析该 json 数据并显示到视图部分。但现在我想从服务器传递多个 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();
}
我想把这三个jso编码的数据传给android
echo json_encode($driverDetails);
echo json_encode($vechicleDetails);
echo json_encode($todaysdata);
当我尝试只将一个 json 数据传递给 android 时,它正在正确我的 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数据???有知道的请回复........