我從Android Hive得到了一個代碼來解析來自url的JSON數據。然後我試圖在Rotten Tomatoes Upcoming Movies Api上實現相同的代碼。根據我的需要,我已經實現了幾乎修改所有xml文件的相同代碼。但問題是當我試圖運行代碼時,它顯示了NetworkOnMainThread Exception。NetworkOnMainThread異常發生
這是我的代碼..
public class Upcoming extends ListActivity
{
String url = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=yvvgsv722gy2zkey3ebkda5t";
final String TAG_MOVIES = "movies";
final String TAG_ID = "id";
final String TAG_TITLE = "title";
final String TAG_YEAR = "year";
final String TAG_MPAA_RATING = "mpaa_rating";
final String TAG_RUNTIME = "runtime";
final String TAG_RELEASE_DATES = "release_dates";
final String TAG_RATINGS = "ratings";
final String TAG_CRITIC_RATING = "critics_ratings";
final String TAG_AUDIENCE_RATING = "audience_ratings";
final String TAG_SYNOPSIS = "synopsis";
final String TAG_POSTERS = "posters";
JSONArray upcomings = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upcoming_list);
ArrayList<HashMap<String, String>> UpcomingList = 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
upcomings = json.getJSONArray(TAG_MOVIES);
// looping through All Contacts
for(int i = 0; i < upcomings.length(); i++){
JSONObject c = upcomings.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String year = c.getString(TAG_YEAR);
String mpaa_rating = c.getString(TAG_MPAA_RATING);
String runtime = c.getString(TAG_RUNTIME);
JSONObject release_dates = c.getJSONObject(TAG_RELEASE_DATES);
JSONObject ratings = c.getJSONObject(TAG_RATINGS);
String critic_rating = c.getString(TAG_CRITIC_RATING);
String audience_rating = c.getString(TAG_AUDIENCE_RATING);
String synopsis = c.getString(TAG_SYNOPSIS);
JSONObject posters = c.getJSONObject(TAG_POSTERS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
map.put(TAG_YEAR, year);
map.put(TAG_CRITIC_RATING, critic_rating);
map.put(TAG_AUDIENCE_RATING, audience_rating);
UpcomingList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, UpcomingList,
R.layout.activity_upcoming,
new String[] { TAG_TITLE, TAG_YEAR, TAG_CRITIC_RATING, TAG_AUDIENCE_RATING }, new int[]
{
R.id.title, R.id.year, R.id.critic_rating, R.id.audience_rating });
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 name = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.year)).getText().toString();
String critic_rating = ((TextView) view.findViewById(R.id.critic_rating)).getText().toString();
String audience_rating = ((TextView) view.findViewById(R.id.audience_rating)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), Upcoming.class);
in.putExtra(TAG_TITLE, name);
in.putExtra(TAG_YEAR, cost);
in.putExtra(TAG_CRITIC_RATING, critic_rating);
in.putExtra(TAG_AUDIENCE_RATING, audience_rating);
startActivity(in);
}
});
}
}
任何人都可以請幫助我什麼我失蹤?我現在完全瞎了。提前Thanx。
http://www.google.co.in/#q=NetworkOnMainThreadexception&spell=1&sa=X&ei=ILTSUeKIBIamrQfrgYGgAg&ved= 0CCcQvwUoAA&BAV = on.2,or.r_qf。&BVM = bv.48705608,d.bmk&FP = 454708d46e718598&BIW = 1152&波黑= 608 –