伙计们!!
在这里,我有一个运行良好的 JSON 解析器,但是当我触摸列表中的 Itme 时,整个应用程序(第二个活动)崩溃了。这段代码有什么问题?有什么想法吗?
First Activity 公开课新闻 extends ListActivity {
// url to make request
private static String url = "http://add.pixelartdev.com/api/get_category_index/?slug=test" +
"";
// JSON Node names
private static final String TAG_POSTS = "posts";
private static final String TAG_PURL = "url";
private static final String TAG_TITLE = "title";
private static final String TAG_EXCERPT = "excerpt";
private static final String TAG_DATE = "date";
// contacts JSONArray
JSONArray posts = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
// Hashmap for ListView
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
posts = json.getJSONArray(TAG_POSTS);
// looping through All Contacts
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String purl = c.getString(TAG_PURL);
String title = c.getString(TAG_TITLE);
String date = c.getString(TAG_DATE);
// Phone number is agin JSON Object
// JSONObject phone = c.getJSONObject(TAG_AUTHOR);
// String mobile = phone.getString(TAG_DATE);
// String home = phone.getString(TAG_CATEGORIES);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PURL, purl);
map.put(TAG_TITLE, title);
map.put(TAG_DATE, date);
// adding HashList to ArrayList
newsList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, newsList,
R.layout.list_item,
new String[] { TAG_TITLE, TAG_DATE, TAG_PURL }, new int[] {
R.id.title, R.id.date, R.id.slugurl, });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String TAG_KURL = ((TextView) view.findViewById(R.id.slugurl)).getText().toString();
String purl = TAG_KURL;
// Starting new intent
Intent postin = new Intent(news.this, post.class);
postin.putExtra(TAG_KURL, purl);
startActivity(postin);
}
});
}
}
第二个活动公共类帖子扩展了活动{
private static String kurl = "TAG_KURL";
// JSON Node names
private static final String TAG_POST = "posts";
private static final String TAG_TITLE = "title";
private static final String TAG_CONTENT = "excerpt";
// contacts JSONArray
JSONArray post = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
// Creating JSON Parser instance
JSONParser jParser = new JSONartParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(getIntent().getStringExtra("TAG_KURL"));
try {
// Getting Array of Contacts
post = json.getJSONArray(TAG_POST);
// looping through All Contacts
for(int i = 0; i < post.length(); i++){
JSONObject c = post.getJSONObject(i);
// Storing each json item in variable
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_CONTENT);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_TITLE, title);
map.put(TAG_CONTENT, content);
TextView lblTitle = (TextView) findViewById(R.id.post_title_label);
TextView lblContent = (TextView) findViewById(R.id.post_content_label);
lblTitle.setText(title);
lblContent.setText(content);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
JSON Parser 公共类 JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}