我使用Volley创建了一个博客阅读器。当你推送List时,我创建了WebView来显示文章(用文章打开WebView),但是我在使用onListItemClick时遇到了问题。问题是:根据我学到的东西,我应该把JSONArray的“名称”,但问题是,我没有这个名字。它看起来像这样:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
JSONArray jsonPosts = obj.getJSONArray(**!!!I don't know what should I put here!!!**);
JSONObject jsonPost = jsonPosts.getJSONObject(position);
String blogUrl = jsonPost.getString(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
} catch (JSONException e) {
Log.e(TAG, "Exception caught!", e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
JSON代码如下所示:
[
{
"id": 58,
"url": "http://integrallab.ru/index.php/categorii-so-statyami/2013-10-25-13-26-29/spiralnaya-dinamika",
"title": "some data",
"time": "15 min",
"author": "name of the author",
"icon": "http://integrallab.hol.es/thumbnail/spiral_dynamics.jpg"
},
{
....................................
MainActivity.java
public class MainActivity extends ListActivity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Posts json url
private static final String url = "http://integrallab.hol.es/document9.json";
private ProgressDialog pDialog;
private List<Post> postList = new ArrayList<Post>();
private ListView listView;
private CustomListAdapter adapter;
private JSONObject obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(android.R.id.list);
adapter = new CustomListAdapter(this, postList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest postReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
obj = response.getJSONObject(i);
Post post = new Post();
post.setTitle(new String(obj.getString("title").getBytes("ISO-8859-1"), "UTF-8"));
post.setThumbnailUrl(obj.getString("icon"));
post.setAuthor(new String(obj.getString("author").getBytes("ISO-8859-1"), "UTF-8"));
post.setTime(new String(obj.getString("time").getBytes("ISO-8859-1"), "UTF-8"));
// adding post to posts array
postList.add(post);
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(postReq);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try {
JSONArray jsonPosts = obj.getJSONArray();
JSONObject jsonPost = jsonPosts.getJSONObject(position);
String blogUrl = jsonPost.getString(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
} catch (JSONException e) {
Log.e(TAG, "Exception caught!", e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
我相信您无需在JSONArray
内完成从{}内部获取onListItemClick
和/或对象的麻烦。
您只需向url
课程添加新字段Post
即可。在里面
public void onResponse(JSONArray response)
您要解析JSONResponse
并将相应的值放入Post
对象字段,同时解析url
。
post.setUrl(new String(obj.getString("url").getBytes("ISO-8859-1"), "UTF-8"));
现在,每个列表项都有Post
个对象,每个Post
对象都有url
,您称之为blogUrl
至于你的项目点击监听器,下面应该满足你的需求
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Post p = (Post) l.getItemAtPosition(position);
String blogUrl = p.getUrl();
Intent intent = new Intent(this, WebViewActivity.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
}
希望这会有所帮助!!