我正在尝试通过从服务器获得的事务列表来填充回收站视图。但除非我放Thread.sleep(7000)
,否则它不会填充。
是否需要这么多时间从服务器端获取数据?如果是的话,还有更快的选择吗?
或从json响应获取字符串并将对象添加到列表是耗时的?因为这个睡眠只是为了在列表中添加5行。当我尝试为整行数运行循环时,我没有得到任何数据。
我的主人是PythonAnywhere。 Api响应在json中,有大约400条记录:
http://sairav.pythonanywhere.com/getTransaction
使用:
Android异步Http客户端::: 编译' 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;
}
}
答案 0 :(得分:0)
使用可用的改造库从json url检索或发布数据...它非常易于使用且效率很高
答案 1 :(得分:0)
如果您确定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);