HorizontalScrollView fetching data from server

2019-09-10 01:06发布

I am trying to implement HorizontalScrollView using this sample code. I have to fetch data from server, so I have made changes as per my requirement, however I am neither getting data nor any error, just blank activity. I don't know what missing.

I am able to get data from server, see logcat result below and here is my complete code:

MainActivity.java

public class MainActivity extends Activity {

    CenterLockHorizontalScrollview centerLockHorizontalScrollview;
    ArrayList<Actors> actorsList;   
    ActorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actorsList = new ArrayList<Actors>();
        new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");

        centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);        
        adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);      
        centerLockHorizontalScrollview.setAdapter(MainActivity.this, adapter);          
    }


    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);


                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("actors");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);

                        Actors actor = new Actors();

                        actor.setName(object.getString("name"));
                        Log.d("Name:", object.getString("name"));
                        actor.setImage(object.getString("image"));
                        Log.d("Image:", object.getString("image"));

                        actorsList.add(actor);
                    }
                    return true;
                }

                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
    }

}

Logcat

10-01 07:28:12.380: I/Process(582): Sending signal. PID: 582 SIG: 9
10-01 07:29:11.280: D/gralloc_goldfish(636): Emulator without GPU emulation detected.
10-01 07:29:12.979: D/Name:(636): Brad Pitt
10-01 07:29:12.979: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg
10-01 07:29:12.979: D/Name:(636): Tom Cruise
10-01 07:29:12.979: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/cruise.jpg
10-01 07:29:12.979: D/Name:(636): Johnny Depp
10-01 07:29:13.009: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/johnny.jpg
10-01 07:29:13.009: D/Name:(636): Angelina Jolie
10-01 07:29:13.019: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/jolie.jpg
10-01 07:29:13.019: D/Name:(636): Tom Hanks
10-01 07:29:13.019: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/tom.jpg
10-01 07:29:13.019: D/Name:(636): Will Smith
10-01 07:29:13.019: D/Image:(636): http://microblogging.wingnity.com/JSONParsingTutorial/will.jpg

1条回答
倾城 Initia
2楼-- · 2019-09-10 01:42

You are setting adapter in onCreate method i mean when task hasn't yet completed. it should be in onPostExecute method

       protected void onPostExecute(Boolean result) {
        dialog.cancel();           
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
        if(actorsList != null) {
            adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);      
            centerLockHorizontalScrollview.setAdapter(MainActivity.this, adapter); 
        }
        adapter.notifyDataSetChanged();
    }
}
查看更多
登录 后发表回答