我已经建立时,我显示数据从URL这样从使用JSON数组请求的URL获取数据的应用程序:显示从URL数据到列表视图
如何可以显示此数据在一个列表视图?这里是我的代码:
public class JsonRequestActivity extends Activity implements OnClickListener {
private String TAG = JsonRequestActivity.class.getSimpleName();
private Button btnJsonObj, btnJsonArray;
private TextView msgResponse;
private ProgressDialog pDialog;
// These tags will be used to cancel the requests
private String tag_json_obj = "jobj_req", tag_json_arry = "jarray_req";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
btnJsonObj = (Button) findViewById(R.id.btnJsonObj);
btnJsonArray = (Button) findViewById(R.id.btnJsonArray);
msgResponse = (TextView) findViewById(R.id.msgResponse);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
btnJsonObj.setOnClickListener(this);
btnJsonArray.setOnClickListener(this);
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}
/**
* Making json object request
* */
private void makeJsonObjReq() {
showProgressDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
Const.URL_JSON_OBJECT, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "[email protected]");
params.put("pass", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq,
tag_json_obj);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
}
/**
* Making json array request
* */
private void makeJsonArryReq() {
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.URL_JSON_ARRAY,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req,
tag_json_arry);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnJsonObj:
makeJsonObjReq();
break;
case R.id.btnJsonArray:
makeJsonArryReq();
break;
}
}
}
这是我使用
http://api.androidhive.info/volley/person_array.json
你到底需要什么帮助?你已经尝试了什么?你的代码中没有提到ListView。 – Egor
我想要像那样的建议,应该为列表视图还是其他活动创建自定义布局?然后我应该在哪里声明arraylist,我怎么可以在ArrayList中显示url来显示它在列表视图 –
恐怕你的问题太广泛了。请搜索文章和教程,解释如何实现您需要的功能,如果您有更具体的问题,请返回。 – Egor