我的PHP Web服务返回多条记录,我的问题是如何在Android中解析它?
<?php
//load and connect to MySQL database stuff
include('../includes/include.inc.php');
?>
<? ############################################################################################# ?>
<?
if (!empty($_POST)) {
$query = "SELECT book_log.`issue_date`,book_log.`due_date`,book_log.`status`,employees.`first_name`,employees.`middle_name`,employees.`last_name`,books.`title` from book_log inner join employees on book_log.`librarian_id`=employees.`id` inner join books on book_log.`book_id`=books.`id` where book_log.`user_id`=(select id from users where username='".$_POST['username']."')";
$info=mysql_query($query) or die (mysql_error());
if(mysql_num_rows($info)>0)
{
while($row=mysql_fetch_array($info))
{
$response["issue_date"]=$row['issue_date'];
$response["due_date"]=$row['due_date'];
$response["status"]=$row['status'];
$response["first_name"]=$row['first_name'];
$response["middle_name"]=$row['middle_name'];
$response["last_name"]=$row['last_name'];
$response["title"]=$row['title'];
$json=json_encode($response);
print_r($json);
}
} else {
$response["success"] = 0;
$response["message"] = "Invalid Student_Id!";
die(json_encode($response));
}
}
?>
<form action="#" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br />
<br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br />
<br />
<input type="submit" value="Login" />
</form>
我创建了一个解析器,但Web服务只返回一行。
我的json解析器是
它只返回最后一条记录, 如何收到所有其他记录,
package com.clockerp.connection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
@SuppressWarnings("unchecked")
public JSONObject makeHttpRequest(String url, String method,
@SuppressWarnings("rawtypes") List params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
// Log.e("HTTP RESPONSE", " result " + is.toString());
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 10);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
json respnce是
03-27 11:12:56.088: E/himanshu(1394): checking
03-27 11:12:56.336: E/himanshu(1394): http://192.168.1.148/clock/webservice/student_libraray_info.php
03-27 11:12:56.337: E/request!(1394): starting
03-27 11:12:57.739: E/HTTP RESPONSE 1is(1394): result org.apache.http.conn.EofSensorInputStream@2ca38b98
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): result
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): {"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Business Statistics"}{"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Marketing Research"}
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <form action="#" method="post">
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): Username:<br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <input type="text" name="username" placeholder="username" />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): Password:<br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <input type="password" name="password" placeholder="password" value="" />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): <input type="submit" value="Login" />
03-27 11:12:57.742: E/HTTP RESPONSE2 sb(1394): </form>
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): result
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): {"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Business Statistics"}{"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Marketing Research"}
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <form action="#" method="post">
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): Username:<br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <input type="text" name="username" placeholder="username" />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): Password:<br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <input type="password" name="password" placeholder="password" value="" />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <br />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): <input type="submit" value="Login" />
03-27 11:12:57.742: E/HTTP RESPONSE 3 json(1394): </form>
03-27 11:12:57.743: E/HTTP RESPONSE 4 jobj(1394): result {"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Business Statistics"}
03-27 11:12:57.744: E/json response(1394): {"issue_date":"2015-03-19","due_date":"2015-04-03","status":"Issued","first_name":"Admin","middle_name":"","last_name":"User","title":"Business Statistics"}
当我将json String转换为json对象时,它只保存第一行。其他行不会来。
答案 0 :(得分:0)
返回json对象,例如对象:
$obj = "[";
whille(mysqli_fetch_array($run$query)){
if($obj!="["){ $obj.="," }
$obj.='{"first_name":"'.$row[1].'",';
$obj.='"second_name":"'.$row[2].'"}';
}
$obj.="]";
return json_encode($obj);
答案 1 :(得分:0)
首先,您不需要手动解析SQL响应,只需让PHP处理它即可。这样,您将获得一个有效的JSON响应,其中所有行都是JSON数组。
$query = "SELECT ...";
$rows = mysql_query($query);
print json_encode($rows);
然后在Android端,您将其解析为JSON数组而不是对象。您不需要手动解析它,只需将整个HTTP响应传递给JSONArray:
JSONArray rows = new JSONArray( response );
for ( int i = 0; i < rows.length(); i++ ) {
JSONObject obe = rows.getJSONObject( i );
}
如果响应中还包含HTML标记,您可能会遇到麻烦,我真的会避免这种情况,只返回JSON。
答案 2 :(得分:-1)
尝试将记录保存在数组中,然后在while循环之外回显它。你的问题是你在同一个变量中保存每一行,这就是它返回的原因 最后一排。
$data=array();
if(mysql_num_rows($info)>0)
{
while($row=mysql_fetch_array($info))
{
$response["issue_date"]=$row['issue_date'];
$response["due_date"]=$row['due_date'];
$response["status"]=$row['status'];
$response["first_name"]=$row['first_name'];
$response["middle_name"]=$row['middle_name'];
$response["last_name"]=$row['last_name'];
$response["title"]=$row['title'];
$data[]=$response;
}
$json=array('data'=>$data);
echo json_encode($data);
}