我是Android新手。我试图通过截击请求从数据库中收集数据。但是,该计划在价值进入之前继续进行。任何人都有解决方案来解决这个问题吗?我用断点来检查程序的顺序。在调用onSuccess函数之前,它将在DatabaseCategory中运行return categoryList。
这是调用DatabaseCategory
的类public class DatabaseFacade implements Database {
private DatabaseCategory databaseCategory;
public DatabaseFacade(Context context) {
databaseCategory = new DatabaseCategory(context);
}
@Override
public List<Category> readCategory() {
return (List<Category>) databaseCategory.loadData(DatabaseUrl.getCategoryReadUrl(), null);
}
}
这是加载数据的类
public class DatabaseCategory extends DatabaseAbstract{
private Context context;
private List<?> categoryList;
public DatabaseCategory(Context context) {
this.context = context;
}
@Override
public List<?> loadData(String url, Object object) {
databaseLoad = new DatabaseLoad(context, ObjectType.CATEGORY, url);
databaseLoad.execute(new CallBack() {
@Override
public void onSuccess(List<?> list) {
categoryList = list;
}
@Override
public void onFail(String msg) {
// Do Stuff
}
});
return categoryList;
}
这是处理截击请求的类
public class DatabaseLoad {
private static final String FAIL_MESSAGE = "Some error occur. Please try later.";
private static final String STATUS_CODE = "success";
private static final int SUCCESS = 1;
private Context context;
private ObjectType objectType;
private String url;
private JSONObject json;
private List<?> loadList;
public DatabaseLoad(Context context, ObjectType type, String url) {
this.context = context;
this.objectType = type;
this.url = url;
}
public void execute(final CallBack onCallBack) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
JSONObject obj;
try {
obj = new JSONObject(s);
if(obj.getInt(STATUS_CODE)==SUCCESS){
Log.d("logindatabase","success");
switch (objectType) {
case CATEGORY:
loadList = loadCategory(obj);
break;
default:
break;
}
onCallBack.onSuccess(loadList);
}else{
Log.d("logindatabase","fail");
Toast.makeText(context, FAIL_MESSAGE , Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, FAIL_MESSAGE , Toast.LENGTH_LONG).show();
onCallBack.onFail(e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Showing toast
Toast.makeText(context, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Creating parameters
Map<String,String> params;
//Adding parameters
switch (objectType) {
case CATEGORY:
params = loadCategoryParameter();
break;
default:
params = new Hashtable<String, String>();
break;
}
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(context.getApplicationContext());
//Adding request to the queue
requestQueue.add(stringRequest);
}
private Map<String,String> loadCategoryParameter() {
Map<String,String> params = new Hashtable<String, String>();
return params;
}
private List<Category> loadCategory(JSONObject obj) throws JSONException {
List<Category> categoryList = new ArrayList<>();
JSONArray categoryArray ;
categoryArray = obj.getJSONArray("posts");
for (int i = 0; i < categoryArray.length(); i++) {
JSONObject c = categoryArray.getJSONObject(i);
int id = c.getInt("id");
String name = c.getString("name");
String type = c.getString("type");
String image = c.getString("image");
Category category = new Category(id,name,type,"",image);
categoryList.add(category);
}
//Log.d("categorylist",obj.toString());
return categoryList;
}
}