我正在学习Json Parsing我想从url中显示listview中的数据 为此,我做了一些代码,但listview中的数据没有显示哪个来自url,请告诉我我在代码中犯了什么错误。
MainActivity Code
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get Cities JSON
private static String url = "http://14.140.200.186/Hospital/get_city.php";
// JSON Node names
private static final String TAG_CITIES = "Cities";
//private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// Cities JSONArray
JSONArray Cities = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> citylist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
citylist = new ArrayList<HashMap<String, String>>();
// Calling async task to get json
new GetCities().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetCities extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
Cities = jsonObj.getJSONArray(TAG_CITIES);
// looping through All Cities
for (int i = 0; i < Cities.length(); i++) {
JSONObject c = Cities.getJSONObject(i);
//String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
HashMap<String, String> Cities = new HashMap<String, String>();
// adding each child node to HashMap key => value
//Cities.put(TAG_ID, id);
Cities.put(TAG_NAME, name);
// adding contact to Cities list
citylist.add(Cities);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, citylist, R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name,});
setListAdapter(adapter);
}
}
第二类代码是
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;}
在activity_main中我只是在LinearLayout中有一个列表视图 我还有另一个布局文件list_item,其中包含单个texview,用于显示结果..
现在请解决我的问题
Thanku
答案 0 :(得分:1)
更改自:
private static final String TAG_NAME = "name";
到
private static final String TAG_NAME = "city_name";
现在有用了请检查您正在使用的服务。
答案 1 :(得分:1)
更改自:
private static final String TAG_NAME = "name";
要
private static final String TAG_NAME = "city_name";
答案 2 :(得分:0)
你可以这样做:
这
ArrayList<HashMap<String, String>> citylist;
到
ArrayList<String> citylist;
您的循环应如下所示:
for (int i = 0; i < Cities.length(); i++) {
JSONObject c = Cities.getJSONObject(i);
//String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// adding contact to Cities list
citylist.add(name);
}
仅将ArrayList传递给适配器。
编辑2 :
这
ListAdapter adapter = new SimpleAdapter(MainActivity.this, citylist, R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name,});
要
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, citylist);
希望这会对你有所帮助。
答案 3 :(得分:0)
有几个地方可能出现问题。
首先:
} else if (method == GET) {
检查是否满足上述条件。您可能希望将GET
替换为ServiceHandler.GET
。
第二:
setListAdapter(adapter);
您需要编写代码才能使列表适配器可见。
答案 4 :(得分:0)
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**`enter code here`
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MainActivity.this, citylist, R.layout.list_item, new String[] { TAG_NAME}, new int[] { R.id.name,});
list.setAdapter(adapter);
}
希望它会对你有所帮助。