Php 如何更改Android代码以处理MySQL数据库中的两个JSON
我是android开发的新手,一直在努力理解这段代码,但我仍然需要一些帮助来理解JSON是如何处理的。不仅如此,我还想用它做一些稍微不同的事情,并且想知道如何更改此代码来实现它 在我现有的PHP代码中,我查询一个MySQL数据库并生成一个JSON数组:Php 如何更改Android代码以处理MySQL数据库中的两个JSON,php,android,mysql,arrays,json,Php,Android,Mysql,Arrays,Json,我是android开发的新手,一直在努力理解这段代码,但我仍然需要一些帮助来理解JSON是如何处理的。不仅如此,我还想用它做一些稍微不同的事情,并且想知道如何更改此代码来实现它 在我现有的PHP代码中,我查询一个MySQL数据库并生成一个JSON数组: echo json_encode($rows1); 看起来是这样的: [ {"id":"6","rating":"5","created":"2012-11-29 00:00:00"}, {"id":"2","r
echo json_encode($rows1);
看起来是这样的:
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;
public FetchDataTask(FetchDataListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
if(params == null) return null;
// get url from params
String url = params[0];
try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if(entity == null) {
msg = "No response from server";
return null;
}
// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
}
catch(IOException e){
msg = "No Network Connection";
}
return null;
}
@Override
protected void onPostExecute(String sJson) {
if(sJson == null) {
if(listener != null) listener.onFetchFailure(msg);
return;
}
try {
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
// create apps list
List<Application> apps = new ArrayList<Application>();
for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Application app = new Application();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sdf1=new SimpleDateFormat("MMM dd yyyy hh:mm:ss");
app.setCreated(sdf1.format(sdf.parse(json.getString("created"))));
app.setRank(json.getString("rating"));
// add the app to apps list
apps.add(app);
}
//notify the activity that fetch data has been complete
if(listener != null) listener.onFetchComplete(apps);
} catch (JSONException e) {
msg = "Invalid response";
if(listener != null) listener.onFetchFailure(msg);
return;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* This function will convert response stream into json string
* @param is respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
throw e;
}
finally {
try {
is.close();
}
catch (IOException e) {
throw e;
}
}
return sb.toString();
}
}
echo json_encode($rows1);
echo json_encode($rows2);
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
[
{"id":"5","rating":"4","created":"2014-02-11 00:00:00"}
]
我的android代码如下所示:
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;
public FetchDataTask(FetchDataListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
if(params == null) return null;
// get url from params
String url = params[0];
try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if(entity == null) {
msg = "No response from server";
return null;
}
// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
}
catch(IOException e){
msg = "No Network Connection";
}
return null;
}
@Override
protected void onPostExecute(String sJson) {
if(sJson == null) {
if(listener != null) listener.onFetchFailure(msg);
return;
}
try {
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
// create apps list
List<Application> apps = new ArrayList<Application>();
for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Application app = new Application();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sdf1=new SimpleDateFormat("MMM dd yyyy hh:mm:ss");
app.setCreated(sdf1.format(sdf.parse(json.getString("created"))));
app.setRank(json.getString("rating"));
// add the app to apps list
apps.add(app);
}
//notify the activity that fetch data has been complete
if(listener != null) listener.onFetchComplete(apps);
} catch (JSONException e) {
msg = "Invalid response";
if(listener != null) listener.onFetchFailure(msg);
return;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* This function will convert response stream into json string
* @param is respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
throw e;
}
finally {
try {
is.close();
}
catch (IOException e) {
throw e;
}
}
return sb.toString();
}
}
echo json_encode($rows1);
echo json_encode($rows2);
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
[
{"id":"5","rating":"4","created":"2014-02-11 00:00:00"}
]
看起来是这样的:
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;
public FetchDataTask(FetchDataListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
if(params == null) return null;
// get url from params
String url = params[0];
try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if(entity == null) {
msg = "No response from server";
return null;
}
// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
}
catch(IOException e){
msg = "No Network Connection";
}
return null;
}
@Override
protected void onPostExecute(String sJson) {
if(sJson == null) {
if(listener != null) listener.onFetchFailure(msg);
return;
}
try {
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
// create apps list
List<Application> apps = new ArrayList<Application>();
for(int i=0; i<aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
Application app = new Application();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sdf1=new SimpleDateFormat("MMM dd yyyy hh:mm:ss");
app.setCreated(sdf1.format(sdf.parse(json.getString("created"))));
app.setRank(json.getString("rating"));
// add the app to apps list
apps.add(app);
}
//notify the activity that fetch data has been complete
if(listener != null) listener.onFetchComplete(apps);
} catch (JSONException e) {
msg = "Invalid response";
if(listener != null) listener.onFetchFailure(msg);
return;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* This function will convert response stream into json string
* @param is respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
throw e;
}
finally {
try {
is.close();
}
catch (IOException e) {
throw e;
}
}
return sb.toString();
}
}
echo json_encode($rows1);
echo json_encode($rows2);
[
{"id":"6","rating":"5","created":"2012-11-29 00:00:00"},
{"id":"2","rating":"5","created":"2013-11-19 00:00:00"},
{"id":"8","rating":"4","created":"2013-06-16 00:00:00"},
{"id":"3","rating":"4","created":"2013-11-05 00:00:00"},
{"id":"10","rating":"3","created":"2012-12-05 00:00:00"},
{"id":"7","rating":"3","created":"2013-08-02 00:00:00"},
{"id":"4","rating":"3","created":"2013-08-12 00:00:00"},
{"id":"9","rating":"2","created":"2013-10-03 00:00:00"}
]
[
{"id":"5","rating":"4","created":"2014-02-11 00:00:00"}
]
如何更改android代码我必须处理两个数组。我的目标是用原始$rows1数组填充Listview,用$rows2填充另一个布局?而不是一次打印两个json字符串尝试将这两个数组转换为单个数组,然后像下面那样打印json字符串 $result=数组($rows1,$rows2)
echo json_编码($result) 您可以将两个
jsonarray
添加到单个Jsonobject
中,并将单个Json对象从服务器发送到android,而android将只发送一个
echo json_encode($rows);
解决方案非常简单。您可以执行以下任一操作:
- 对于每行提取,可以使用不同的参数url调用FetchDataTask
- 您可以将FetchDataTask
更改为循环遍历一个参数数组,您可以为每一行提供该数组,然后获取这些行。e、 gdoInBackground
然后返回一个字符串结果数组,在您的row1Url=params[0],row2Url=params[1]…
中循环它们,并填充各自的布局onPostExecute