我是新來的android開發人員,並且一直在努力將此代碼包裹起來,但我仍然需要一些幫助來理解如何處理這個JSON。不僅如此,我想做一些與它稍有不同的事情,並想知道如何改變這個代碼來做到這一點。如何更改Android代碼以從MySQL數據庫中處理兩個JSON
在我現有的PHP代碼,我查詢MySQL數據庫,並作出JSON陣列:
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"}
]
我的Android代碼如下所示:
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();
}
}
現在,讓我們說我想在PHP中創建兩個MySQL數據庫查詢並創建兩個JSON數組,如thi S:
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填充另一個佈局?的