0

Im developing an app using eclipse and retrofit that is designed to get a JSON array of objects and present it in a listview. My webservice works fine.I am using retrofit2 to get POJO class as follows:

public static UnicsAgencyApi getUnicsAgencyApi() {

        if (sUnicsAgencyApi == null) {
            retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL).addConverterFactory(GsonConverterFactory.create())
                    .build();
            sUnicsAgencyApi = retrofit.create(UnicsAgencyApi.class);
        }
        return sUnicsAgencyApi;
    }

    public interface UnicsAgencyApi {

        @GET("api/uconnectservice/AllAgency")
        Call<ArrayList<AgencyModel>> getStreams();
    }

and here is where i call:

public void ShowAllUnicsAgencyInfo() {

        // get prompts.xml view
        LayoutInflater layoutInflater = LayoutInflater.from(Homepage.this);
        final View promptView = layoutInflater.inflate(R.layout.view_agency_list, null);
        listView2 = (ListView) promptView.findViewById(R.id.list);
        // GET DATA FROM WEBSERVICE
        ArrayList<AgencyModel> AgencyList = null;

        AgencyList = DownloadAgencyData();

        **AgencyListAdapter mAdapter = new AgencyListAdapter(this, AgencyList);**
        listView2.setAdapter(mAdapter);

        // ListView Item Click Listener
        listView2.setOnItemSelectedListener(new CustomOnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Toast.makeText(parent.getContext(),
                        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT)
                        .show();

            }
        });

        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Homepage.this);
        alertDialogBuilder.setView(promptView);

        // setup a dialog window
        alertDialogBuilder.setCancelable(false).setNeutralButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });
        // create an alert dialog
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();

    }

    public ArrayList<AgencyModel> DownloadAgencyData() {

        RestApi.getUnicsAgencyApi().getStreams().enqueue(new Callback<ArrayList<AgencyModel>>() {

            @Override
            public void onFailure(Call<ArrayList<AgencyModel>> arg0, Throwable arg1) {
                // TODO Auto-generated method stub

                Log.e("Error in parsing", arg0.toString());

            }

            @Override
            public void onResponse(Call<ArrayList<AgencyModel>> AgencyModelData,
                    Response<ArrayList<AgencyModel>> response) {
                // TODO Auto-generated method stub
                mstreamData = new ArrayList<AgencyModel>();
                // ADD TO List here!!!!!!!!
                // Log.e("Response", "" + response.body().size());
                **mstreamData.addAll(response.body());**

            }

        });
        return mstreamData;
    }

However the error i keep getting is from custom array adapter getCount() method via following Logcat:

 01-03 02:48:41.615: E/test(30453): Exception
    01-03 02:48:41.635: E/AndroidRuntime(30453): FATAL EXCEPTION: main
    01-03 02:48:41.635: E/AndroidRuntime(30453): Process: com.nickSoft.unics_alpha, PID: 30453
    01-03 02:48:41.635: E/AndroidRuntime(30453): java.lang.NullPointerException
    01-03 02:48:41.635: E/AndroidRuntime(30453):    at com.nickSoft.unics_alpha.AgencyListAdapter.getCount(AgencyListAdapter.java:26)
    01-03 02:48:41.635: E/AndroidRuntime(30453):    at android.widget.ListView.setAdapter(ListView.java:480)
    01-03 02:48:41.635: E/AndroidRuntime(30453):    at com.nickSoft.unics_alpha.Homepage.ShowAllUnicsAgencyInfo(Homepage.java:546)

Please i can't seem to identify where i went wrong,any guidance as to what could be the error or better way of solving this prob is greatly appreciated,cheers

user3015410
  • 29
  • 1
  • 9

2 Answers2

0

RestApi.getUnicsAgencyApi().getStreams().enqueue method more likely work on thread.so the download... method return datas will not be correctly. you can callbakc the result in onResponse method.

empt
  • 133
  • 1
  • 10
  • Ok,@empt but can u be more explicit pls – user3015410 Jan 03 '17 at 03:27
  • first let you adapter to be Globally. and remove the code and onReponse like this '@Override public void onResponse(Call> AgencyModelData, Response> response) { if (response == null) response = new ArrayList<>(); mAdapter = new AgencyListAdapter(this, AgencyList); listView2.setAdapter(mAdapter); }' – empt Jan 03 '17 at 03:40
  • remove the code `ArrayList AgencyList = null; AgencyList = DownloadAgencyData(); **AgencyListAdapter mAdapter = new AgencyListAdapter(this, AgencyList);** listView2.setAdapter(mAdapter);` **replaced** DownloadAgencyData() – empt Jan 03 '17 at 03:47
0

I guess your AgencyList is not instantiated or is pointing to null even after the call

AgencyList = DownloadAgencyData();

Add a breakpoint at that line to debug.

Since your listview depends on data from a webservice its good to check if data was received or not before call to setAdapter(); -

if(AgencyList !=null){
 **AgencyListAdapter mAdapter = new AgencyListAdapter(this, AgencyList);**
        listView2.setAdapter(mAdapter);
} else{ Log.d(TAG,"AgencyList is null");

Edit: See if this helps Set multiple attempts for accessing data.

for( int retry=0; AgencyList!=null && retry <10; retry++){ AgencyList = DownloadAgencyData(); }

sziraqui
  • 5,763
  • 3
  • 28
  • 37
  • i appreciate that piece of code.Now i get AgencyList is null message which suggests nothing is recieved from DownloadAgencyData(),I rechecked my webservice everything is fine.....so close but so far....... – user3015410 Jan 03 '17 at 03:30
  • Set multiple attempts for accessing data. You can do `for(retry=0;AgencyList!=null && retry <10; retry++){ // your Download logic } if retry>10 and AgencyList==null then you may need to check your network connection. Have you declared Network access permission in your AndroidManifest.xml ? Does the web service use https connection – sziraqui Jan 03 '17 at 05:48
  • i'm using ASP.NET Web Api as webservice,and i have all the necessary permissions in my manifest and the connection ok. Still i can't get the list to show ...so close but so far .... – user3015410 Jan 03 '17 at 10:52