如何使用Android从Mysql获取JSON数组
这是我的json数组如何使用Android从Mysql获取JSON数组,android,json,Android,Json,这是我的json数组 { "getRestorentList": { "status": "YES", "getRestorentList": [ { "business_id": "93", "business_name": "Johnny's Steakhouse ", "description": "Johnny's Steakho
{
"getRestorentList": {
"status": "YES",
"getRestorentList": [
{
"business_id": "93",
"business_name": "Johnny's Steakhouse ",
"description": "Johnny's Steakhouse , Is one of the best restaurant in our robust restaurant search engine. We help diners easily find the best restaurant for every occasion, every time from all cities in Germany from the most elegant restaurants for fine dining to casual, inexpensive spots for family meals, you'll find it all on 123gotit.com",
"business_address": "Berliner Str. 28",
"phone": "234-423-3242"
},
{
"business_id": "94",
"business_name": "Name of Restaurant",
"description": "Name of Restaurant, Is one of the best restaurant in our robust restaurant search engine. We help diners easily find the best restaurant for every occasion, every time from all cities in Germany from the most elegant restaurants for fine dining to casual, inexpensive spots for family meals, you'll find it all on 123gotit.com",
"business_address": "Address",
"phone": "345-534-5345"
}
]
}
}
new golfSync().execute();
ListView lv=getListView();
}
类golfSync扩展异步任务{
ArrayList列表数组;
JSONArray MemberlistJSONArray;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(AtoZActivity.this);
setMessage(“正在加载数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
int-datasize;
JSONObject RegistrationResultJSONObject=null;
ArrayList后参数=null;
试一试{
postParameters=新的ArrayList();
日志i(“测试”、“abcccc”);
添加(新的BasicNameValuePair(“成员id”),
"7"));
RegistrationResultJSONObject=新JSONObject(
JsonParser.doFetchDataFromWebService(appsutibility.getRestaurantURL,“getRestorent”,
后参数);
Log.i(“members infohhh”,RegistrationResultJSONObject.toString());
RegistrationResultJSONObject=RegistrationResultJSONObject
.getJSONObject(“getRestorentList”);
if(RegistrationResultJSONObject.get(“status”).equals(“YES”)){
memeberListjsonArray=注册结果JSONObject
.getJSONArray(“getRestorentList”);
datasize=memeberListjsonArray.length();
HashMap=newHashMap();
Log.i(“数据数组大小”,datasize+);
listArray=新的ArrayList(数据大小);
对于(int i=0;ivalue
地图放置(“业务名称”,名称);
地图放置(“说明”,说明);
地图放置(“业务地址”,地址);
RetailerList.add(地图);
}
//arrayadapter=新的arrayadapter(GolfRetailerActivity.this,R.layout.list\u golfretailer,R.id.retailername\u tv,memberNameArray);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
pDialog.disclose();
//从后台线程更新UI
适配器=新的SimpleAdapter(AtoZActivity.this,
零售店列表,右布局,餐厅列表,
新字符串[]{“业务名称”、“说明”、“业务地址”},
新int[]{R.id.restaurant_name_textView2,R.id.restaurant_description_textView3,R.id.restaurant_address_textView4});
//更新列表视图
setListAdapter(适配器);
}
这是我的最后一段代码使用php脚本查询数据库并输出到jSON。 对不起,我不是100%确定问题是什么 这里有一个教程
您可能需要添加一个问题-它是否有效?如果无效,请发布logcat。
new golfSync().execute();
ListView lv = getListView();
}
class golfSync extends AsyncTask<Void, Void, Void> {
ArrayList<DataModel> listArray;
JSONArray memeberListjsonArray;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AtoZActivity.this);
pDialog.setMessage("Loading Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
int datasize;
JSONObject RegistrationResultJSONObject = null;
ArrayList<NameValuePair> postParameters = null;
try {
postParameters = new ArrayList<NameValuePair>();
Log.i("test", "abcccc");
postParameters.add(new BasicNameValuePair("member_id",
"7"));
RegistrationResultJSONObject = new JSONObject(
JsonParser.doFetchDataFromWebService(AppsUtility.getrestaurantlurl, "getRestorent",
postParameters));
Log.i("members infohhhh", RegistrationResultJSONObject.toString());
RegistrationResultJSONObject = RegistrationResultJSONObject
.getJSONObject("getRestorentList");
if (RegistrationResultJSONObject.get("status").equals("YES")) {
memeberListjsonArray = RegistrationResultJSONObject
.getJSONArray("getRestorentList");
datasize = memeberListjsonArray.length();
HashMap<String, String> map = new HashMap<String, String>();
Log.i("Data Array Size", datasize + "");
listArray = new ArrayList<DataModel>(datasize);
for (int i = 0; i < datasize; i++) {
JSONObject dataobj = memeberListjsonArray
.getJSONObject(i);
Log.i("i", i + "");
String name = dataobj.getString("business_name");
Log.i("business_name", name);
String description = dataobj.getString("description");
Log.i("id", description);
String address = dataobj.getString("business_address");
Log.i("business_name", address);
// adding each child node to HashMap key => value
map.put("business_name", name);
map.put("description", description);
map.put("business_address", address);
RetailerList.add(map);
}
// arrayadapter = new ArrayAdapter<String>(GolfRetailerActivity.this, R.layout.list_golfretailer,R.id.retailername_tv, memberNameArray);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
pDialog.dismiss();
// updating UI from Background Thread
adapter = new SimpleAdapter(AtoZActivity.this,
RetailerList, R.layout.list_restaurant,
new String[] { "business_name","description","business_address"},
new int[] { R.id.restaurant_name_textView2,R.id.restaurant_description_textView3,R.id.restaurant_address_textView4 });
// updating listview
setListAdapter(adapter);
}