我的应用程序有一个SQlite数据库,我试图在服务器的初始安装时填充。我正在使用凌空连接并在服务器端拥有一个Web服务。我的问题是,在第一次安装时,当我调用方法来填充数据库时,它显然是在不同的线程上运行,所以在完成它之前,正在构建UI。 UI是从数据库填充的,因为数据库尚未填充填充,我得到错误。
我试图强制凌空调用同步运行但只是获得超时异常
11-29 09:38:49.646 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: java.util.concurrent.TimeoutException
11-29 09:38:49.646 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.android.volley.toolbox.RequestFuture.doGet(RequestFuture.java:121)
11-29 09:38:49.646 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.android.volley.toolbox.RequestFuture.get(RequestFuture.java:97)
11-29 09:38:49.646 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.clashtoolkit.clashtoolkit.network.ArmyNetworking.getTableFromServer(ArmyNetworking.java:93)
11-29 09:38:49.646 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.clashtoolkit.clashtoolkit.database.DataCreator.getDatabaseFromServer(DataCreator.java:1657)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.clashtoolkit.clashtoolkit.UI.ArmyBuilder.onCreate(ArmyBuilder.java:46)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 09:38:49.647 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.os.Looper.loop(Looper.java:135)
11-29 09:38:49.648 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5257)
11-29 09:38:49.648 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-29 09:38:49.648 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-29 09:38:49.648 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-29 09:38:49.648 4206-4206/com.clashtoolkit.clashtoolkit W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我无法弄清楚这样做的最佳方法。必须首先填充数据库,但我似乎无法阻止UI代码在填充之前运行。
Main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_army_builder);
//create and populate the database
DataCreator dbCreate = new DataCreator();
dbCreate.getDatabaseFromServer(this);
//creates the Toolbar and setting it as the Toolbar for the activity
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new TroopPagerAdapter(getSupportFragmentManager(), titles, numberOfTabs);
//Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.army_pager);
pager.setOffscreenPageLimit(4);
pager.setAdapter(adapter);
//assigning the sliding tab layout view
tabs = (SlidingTabLayout) findViewById(R.id.army_tabs);
//creating the icon portion of the sliding tabs
tabs.setCustomTabView(R.layout.custom_tabs, R.id.tabText);
//Setting custom color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.ColorPrimaryDark);
}
});
//Setting the ViewPager for the slidingtabsLayout
tabs.setViewPager(pager);
//Set the barracks fragments to the barracks viewpager
BuilderObjData builderObjData = new BuilderObjData();
ArrayList<Builder> barracksArray = builderObjData.getBuilderObjArray(this, Utilities.BUILDER_TYPE_BARRACKS);
bAdpter = new BarracksPagerAdapter(getSupportFragmentManager(), barracksArray);
bPager = (ViewPager) findViewById(R.id.barracks_pager);
bPager.setAdapter(bAdpter);
}
getDatabaseFromServer()
public void getDatabaseFromServer(Context context) {
Cursor cursor = context.getContentResolver().query(Container.CONTENT_URI, null, null, null, null);
if (cursor == null || cursor.getCount() == 0) {
for (String table : DataCreator.TABLES) {
ArmyNetworking.getTableFromServer(table, context);
}
if (cursor != null) {
cursor.close();
}
}
}
getTableFromServer()
public static void getTableFromServer(final String tableName, final Context context) {
Log.d(TAG, "called now");
String url = ArmyNetworking.WEB_BASE_URL + ArmyNetworking.PATH_TABLE + "/" + tableName;
// RequestFuture<JSONArray> future = RequestFuture.newFuture();
// JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, future, future);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
url,
(String) null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
updateTableFromServer(response, context, tableName);
Log.d(TAG, "got response");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//add error handling
}
});
VolleySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
// try {
// JSONArray response = future.get(30, TimeUnit.SECONDS);
// updateTableFromServer(response, context, tableName);
//
// } catch (InterruptedException | ExecutionException | TimeoutException e) {
// e.printStackTrace();
// }
}
上面的方法显示了我尝试进行Volley调用的两种不同方式。注释掉的代码是强制它同步
答案 0 :(得分:2)
只需在程序开头添加另一个活动,其中会显示一个进度条和一个文本,例如“请等待数据从网络加载,或者在此期间查看竞争对手的应用程序”。