I got a code from Android Hive to parse JSON data from url. Then I am trying to implement the same code on Rotten Tomatoes Upcoming Movies Api. I have implemented the same code with almost modifying all the xml files according to my needs. But the problem is when I am trying to run the code, its showing NetworkOnMainThread Exception.
This is my code..
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);
}
});
}
}
Can anyone please help me with anything I am missing.? I am totally blind on it now. Thanx in advance.