0

I am trying to populate RecyclerView by the list of transactions i get from the server . but unless i put a Thread.sleep(7000) , it won't populate . Does it take this much time to get data from server side ? If yes , Is there any faster alternative ?

or is getting the string from json response and adding object to list is time consuming ? because this sleep is just working for adding 5 rows in list. when i try to run loop for whole number of rows i don't get any data .

My host is PythonAnywhere . API response is in json and has around 400 records :

http://sairav.pythonanywhere.com/getTransaction

Using :

Android Asynchronous Http Client::: compile 'com.loopj.android:android-async-http:1.4.9'

public List<Transaction> getTransactions(final boolean getAll) {
        Thread bgThread =null;
      
       final List<Transaction> trList=new ArrayList<>();
        RequestParams requestParams = new RequestParams();

        requestParams.put("uid", Profile.getCurrentProfile().getId());

        PAAPI.post("/getTransaction", requestParams, new JsonHttpResponseHandler() {

 @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray jsonArray) {
                
                Transaction trr = null;
                if (getAll) {
                   

                    for (int i = 0; i < 5; i++) {

                        try {
                            //String a = jsonArray.getString(i);
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            //JSONArray arrayWithElements = jsonObject.toJSONArray(new JSONArray(new String[]{"name","desc","amount","ttype","uid","ttime"}));

                            trr =  new Transaction(context);

                            trr.uname = jsonObject.getString("uname");
                            trr.desc = jsonObject.getString("description");
                            trr.amount = jsonObject.getString("amount");
                            trr.type = jsonObject.getString("type");
                            trr.uid = jsonObject.getString("uid");
                            trr.date = jsonObject.getString("ttime");

                            trList.add(trr);
                           // Toast.makeText(context,"size is bro :"+trList.size(),Toast.LENGTH_SHORT).show();
                            if (i == 1) {
                   //             Toast.makeText(context, trr.uname + "-" + desc + "-" + trr.amount + "-" + trr.type + "-" + trr.uid + "-" + trr.date, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // Do something with the response

                } 
 });
        try {
           Toast.makeText(context,"sleeping bo",Toast.LENGTH_SHORT).show();

           Thread.sleep(7000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

      //  Toast.makeText(context, "listsize final is" +  trList.size(), Toast.LENGTH_SHORT).show();
        return  trList;
    }



class PAAPI {
    protected static final String BASE_URL = "http://sairav.pythonanywhere.com";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
devcodes
  • 1,038
  • 19
  • 38

2 Answers2

0

Use the retrofit library available to retrieve or post the data from a JSON URL ...it is very easy to use and is efficient

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
ch3125
  • 31
  • 3
  • i actually now used AsyncTask instead of loopj APIs , and it seems to be working quite good . No delay. Will give a try to retrofit also . thanks . – devcodes Dec 04 '16 at 07:20
0

If you are certain that getString() operation is taking too much time to perform then you can use progress dialog instead of using Thread.sleep()

private class PAAPI extends AsyncTask<Boolean, Void, List<Transaction> {

   ProgressDialog dialog = new ProgressDialog(MainActivity.this);

   @Override
   protected void onPreExecute() {
      //set message of the dialog
      dialog.setMessage("Loading...");
      //show dialog
      dialog.show();
      super.onPreExecute();
   }

   protected Void doInBackground(Boolean... args) {
      // do background work here
      return null;
   }

   protected void onPostExecute(List<Transaction> result) {
     // do UI work here
     if(dialog != null && dialog.isShowing()){
       dialog.dismiss()
     }
   }
}

and later use it as new PAAPI().execute(getAll);

Shubham
  • 165
  • 2
  • 11