gpt4 book ai didi

android - Json 数据未显示在 ListView 中

转载 作者:行者123 更新时间:2023-11-29 20:06:48 25 4
gpt4 key购买 nike

我正在学习 Json 解析我想在 ListView 中显示来自 url 的数据为此我做了一些代码,但 ListView 中的数据没有显示哪个来自 url 请告诉我我在代码中犯了什么错误。

MainActivity 代码为

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 中有一个 ListView 我还有另一个布局文件 list_item,带有单个 texview 用于显示结果..

现在请解决我的问题

谢谢

最佳答案

更改自:

     private static final String TAG_NAME = "name";

   private static final String TAG_NAME = "city_name";

现在会有帮助请检查您正在使用的服务。

关于android - Json 数据未显示在 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35546877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com