-4

Below is my code:

private static final String TAG_TYPE = "movie_type";
private static final String TAG_NAME = "movie_name";
private static final String TAG_LENGTH = "movie_length";
private static final String TAG_SCHEDULES = "movie_schedules";
private static final String TAG_CINEMA = "movie_cinema_number";
private static final String TAG_URL = "movie_image_url";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


            try {

                String readMovieSchedules = readMovieSchedules();

                // Hashmap for ListView
                ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();

                JSONArray jsonArray = new JSONArray(readMovieSchedules);
                Log.i(MainActivity.class.getName(),
                    "Number of entries " + jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                  JSONObject jsonObject = jsonArray.getJSONObject(i);
                  Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));

                  // Storing each json item in variable
                  String name = jsonObject.getString(TAG_NAME);
                  String type = jsonObject.getString(TAG_TYPE);
                  String length = jsonObject.getString(TAG_LENGTH);
                  String cinema = jsonObject.getString(TAG_CINEMA);
                  String schedules = jsonObject.getString(TAG_SCHEDULES);
                  String url = jsonObject.getString(TAG_URL);

                  // creating new HashMap
                  HashMap<String, String> map = new HashMap<String, String>();

                  // adding each child node to HashMap key => value
                  map.put(TAG_NAME, name);
                  map.put(TAG_TYPE, type);
                  map.put(TAG_LENGTH, length);
                  map.put(TAG_CINEMA, cinema);
                  map.put(TAG_SCHEDULES, schedules);
                  map.put(TAG_URL, url);

                  // adding HashList to ArrayList
                  movieList.add(map);

                  //String strURL = TAG_URL.replaceAll(" ", "%20");

                  /**
                   * Updating parsed JSON data into ListView
                   * */
                  ListAdapter adapter = new SimpleAdapter(MainActivity.this, movieList,
                        R.layout.list_item,
                        new String[] { TAG_NAME, TAG_CINEMA, TAG_SCHEDULES }, 
                        new int[] { R.id.name, R.id.cinema, R.id.schedules }); 
                        //new String[] {},
                        //new int[] {});

                  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.name)).getText().toString();
                          String cost = ((TextView) view.findViewById(R.id.cinema)).getText().toString();
                          String description = ((TextView) view.findViewById(R.id.schedules)).getText().toString();
                          //String url = ((TextView) view.findViewById(R.id.image_)).getText().toString();

                          // Starting new intent
                          Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                          in.putExtra(TAG_NAME, name);
                          in.putExtra(TAG_CINEMA, cost);
                          in.putExtra(TAG_SCHEDULES, description);
                          //in.putExtra(TAG_URL, url);
                          startActivity(in);
                      }
                  });
                }
            } catch (Exception e) {
              e.printStackTrace();
            }

}

This code is working on SDK 8, but not working on 17. I'm stuck with this problem. Does anyone here know how to debug this? I'd gladly appreciate your help. thanks.

neknek mouh
  • 1,802
  • 8
  • 27
  • 58

3 Answers3

2

You can't make network calls on the main thread since API 11 (3.0). Read more here: http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

bclymer
  • 6,679
  • 2
  • 27
  • 36
1

You have to put your code inside an AsyncTask , it is forbidden to download anything on the main UI thread since API 14 I believe.

It would be something like this:

public class someTask extends AsyncTask<String, Void, String> {
    public MainActivity activity;
    public someTask(MainActivity a)
    {
        activity = a;
    }
    @Override
            protected String doInBackground(String... urls) {

            String stringtoparse=null;
                    for (String url : urls) {
                            stringtoparse= readMovieSchedules(url); // getting XML from URL
                            }
                    return stringtoparse;
            }

    @Override
    protected void onPreExecute(){
    }

@Override
    protected void onPostExecute(String readMovieSchedules) {

// Hashmap for ListView
            ArrayList<HashMap<String, String>> movieList = new ArrayList<HashMap<String, String>>();

            JSONArray jsonArray = new JSONArray(readMovieSchedules);
            Log.i(MainActivity.class.getName(),
                "Number of entries " + jsonArray.length());
            for (int i = 0; i < jsonArray.length(); i++) {
              JSONObject jsonObject = jsonArray.getJSONObject(i);
              Log.i(MainActivity.class.getName(), jsonObject.getString("movie_name"));

              // Storing each json item in variable
              String name = jsonObject.getString(TAG_NAME);
              String type = jsonObject.getString(TAG_TYPE);
              String length = jsonObject.getString(TAG_LENGTH);
              String cinema = jsonObject.getString(TAG_CINEMA);
              String schedules = jsonObject.getString(TAG_SCHEDULES);
              String url = jsonObject.getString(TAG_URL);

              // creating new HashMap
              HashMap<String, String> map = new HashMap<String, String>();

              // adding each child node to HashMap key => value
              map.put(TAG_NAME, name);
              map.put(TAG_TYPE, type);
              map.put(TAG_LENGTH, length);
              map.put(TAG_CINEMA, cinema);
              map.put(TAG_SCHEDULES, schedules);
              map.put(TAG_URL, url);

              // adding HashList to ArrayList
              movieList.add(map);

    }
}

You have to modify the readMovieSchedules() to take the url as an argument like this readMovieSchedules(url) and I think it'll work just fine, you would call the task like this:

getMovieSched task = new someTask(MainActivity.this);
task.execute(url);
ah008a
  • 1,115
  • 6
  • 11
1

ASyncTask is specifically made for scenarios like this. It allows you to communicate over the network in a seperate thread without you needing to work with threads.

ASyncTask has these methods :

onPreExecute() : this is invoked before the main processing happens ... one use of it would be to start the progress dialog notifying the user of the transaction.

doInBackground() : this is where you perform data exchange from the network . no UI fiddling around here.

onProgressUpdate() : can be used to notify progress of the transaction

onPostExecute() : dismiss the progressDialogs/bars and update your views from the fetched data!

another solution would be to use runOnUiThread() but this is discouraged!

class nwthread extends AsyncTask<Void,Void,Void>

{
@Override
    protected void onPreExecute() {
//progress dialog invoke ( notifies the user about whats going on better than making them stare on a blank screen)
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        //make http request/parse json here
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
//dismiss progress dialog
        // update the UI here ... ie declare adapters / bind them, update other views

    }
}

finally invoke nwthread.execute(); after setContentView();

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28