从服务器获得数据太慢?(安卓)



我试图通过从服务器获得的交易列表来填充RecyclerView。但是,除非我放了Thread.sleep(7000),否则它不会填充。从服务器端获取数据需要花费太多时间吗?如果是,是否有更快的替代方法?

还是从 json响应中获取字符串,并将对象添加到列表中是很耗时的?因为这种睡眠只是为了在列表中添加5行。当我尝试为整数行运行循环时,我没有获得任何数据。

我的主人是pythonanywhere。API响应在json中,有大约400个记录:

http://sairav.pythonanywhere.com/getTransaction

使用:

Android异步HTTP客户端::::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;
    }
}

如果您确定getString()操作需要太多时间进行执行,则可以使用进度对话框而不是使用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()
     }
   }
}

然后以后用作new PAAPI().execute(getAll);

使用可用于检索或从 JSON URL中发布数据的改装库...它非常易于使用并且有效

最新更新