我试图通过ParseQuerys类中的方法调用AsyncTask,以便在我的Fragment lvRestaurantFrag的ListView中使用该信息。问题是该方法返回null,因为AsyncTask没有到达onPostExecute。我不知道如何解决它。
我留下我的代码:
[
{
"idBranchRestaurant":1,
"idRestaurant":1,
"restaurant":{
"idRestaurant":1,
"name":"Don Julio",
"description":"La mejor parrilla para cualquier turista",
"socialNetwork":[
{
"idSocialNetworkRestaurant":1,
"idRestaurant":1,
"idTypeSocialNetwork":1,
"value":"www.donjulio.com.ar",
"type":{
"idTypeSocialNetwork":1,
"name":"Website",
"icon":"website_ic.jpg"
}
}
]
},
"idRangePriceBranch":1,
"RangePrice":{
"idRangePriceBranch":1,
"idBranchRestaurant":1,
"minimum":300,
"maximum":600
},
"name":"Paraguay",
"photo":[
{
"idBranchPhoto":4,
"idBranchRestaurant":1,
"branch":null,
"idUser":0,
"user":null,
"photo":"entradaDonJulio.jpg"
}
],
"cuisine":[
{
"idCousine":1,
"name":"Parrilla"
}
],
"menu":[
{
"idTypeMenu":1,
"name":"Mediodia",
"idBranchRestaurant":1,
"plates":null
},
{
"idTypeMenu":2,
"name":"Noche",
"idBranchRestaurant":1,
"plates":null
}
],
"filter":[
{
"idTypeFilter":2,
"name":"Family",
"icon":"family_ic.jpg"
}
],
"service":[
{
"idService":2,
"name":"Comida para celiacos"
},
{
"idService":1,
"name":"Wifi"
}
],
"timetable":[
{
"idTimetableBranch":11,
"idBranchRestaurant":1,
"idDay":2,
"openingHour":"19 hs",
"closingHour":"24 hs",
"day":{
"idDay":2,
"name":"Tuesday"
}
},
{
"idTimetableBranch":10,
"idBranchRestaurant":1,
"idDay":2,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":2,
"name":"Tuesday"
}
},
{
"idTimetableBranch":9,
"idBranchRestaurant":1,
"idDay":1,
"openingHour":"19 hs",
"closingHour":"24 hs",
"day":{
"idDay":1,
"name":"Monday"
}
},
{
"idTimetableBranch":8,
"idBranchRestaurant":1,
"idDay":1,
"openingHour":"12 hs ",
"closingHour":"16 hs",
"day":{
"idDay":1,
"name":"Monday"
}
},
{
"idTimetableBranch":12,
"idBranchRestaurant":1,
"idDay":3,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":3,
"name":"Wednesday"
}
},
{
"idTimetableBranch":13,
"idBranchRestaurant":1,
"idDay":3,
"openingHour":"19 hs ",
"closingHour":"24 hs",
"day":{
"idDay":3,
"name":"Wednesday"
}
},
{
"idTimetableBranch":14,
"idBranchRestaurant":1,
"idDay":4,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":4,
"name":"Thursday"
}
},
{
"idTimetableBranch":15,
"idBranchRestaurant":1,
"idDay":4,
"openingHour":"19 hs",
"closingHour":"24 hs",
"day":{
"idDay":4,
"name":"Thursday"
}
},
{
"idTimetableBranch":16,
"idBranchRestaurant":1,
"idDay":5,
"openingHour":"12 hs",
"closingHour":"16 hs ",
"day":{
"idDay":5,
"name":"Friday"
}
},
{
"idTimetableBranch":17,
"idBranchRestaurant":1,
"idDay":5,
"openingHour":"19 hs ",
"closingHour":"2 hs",
"day":{
"idDay":5,
"name":"Friday"
}
}
],
"latitude":"87654322",
"longitude":"23456789",
"calification":null,
"averageFood":4.0,
"averageService":4.0,
"averageAmbience":4.0,
"averageCalification":4.0,
"promotion":[
{
"idPromotion":3,
"idTypePromotion":2,
"code":"DIADELPADRE",
"name":"Dia Del Padre Regalo",
"startDate":"2017-06-26T00:00:00",
"expireDate":"2017-06-18T00:00:00",
"description":"postre de regalo",
"value":100,
"type":{
"idTypePromotion":2,
"name":"Gift"
}
}
]
},
{
"idBranchRestaurant":2,
"idRestaurant":2,
"restaurant":{
"idRestaurant":2,
"name":"La Hormiga",
"description":"el lugar explota de gente y en especial de gente de ort",
"socialNetwork":[
{
"idSocialNetworkRestaurant":2,
"idRestaurant":2,
"idTypeSocialNetwork":2,
"value":"www.facebook.com/LaHormiga",
"type":{
"idTypeSocialNetwork":2,
"name":"Facebook",
"icon":"fb_ic.jpg"
}
}
]
},
"idRangePriceBranch":1,
"RangePrice":{
"idRangePriceBranch":1,
"idBranchRestaurant":1,
"minimum":300,
"maximum":600
},
"name":"Ort",
"photo":[
{
"idBranchPhoto":5,
"idBranchRestaurant":2,
"branch":null,
"idUser":0,
"user":null,
"photo":"entradaLaHormiga.jpg"
}
],
"cuisine":[
{
"idCousine":2,
"name":"Pizza"
}
],
"menu":[
{
"idTypeMenu":3,
"name":"Mediodia",
"idBranchRestaurant":2,
"plates":null
}
],
"filter":[
{
"idTypeFilter":1,
"name":"With Discount",
"icon":"discount_ic.jpg"
}
],
"service":[
{
"idService":3,
"name":"Aire acondicionado"
},
{
"idService":1,
"name":"Wifi"
}
],
"timetable":[
{
"idTimetableBranch":18,
"idBranchRestaurant":2,
"idDay":6,
"openingHour":"11 hs",
"closingHour":"15 hs",
"day":{
"idDay":6,
"name":"Saturday"
}
},
{
"idTimetableBranch":19,
"idBranchRestaurant":2,
"idDay":7,
"openingHour":"11 hs",
"closingHour":"15 hs",
"day":{
"idDay":7,
"name":"Sunday"
}
}
],
"latitude":"234567897",
"longitude":"123456789",
"calification":null,
"averageFood":5.0,
"averageService":5.0,
"averageAmbience":5.0,
"averageCalification":5.0,
"promotion":[
]
},
{
"idBranchRestaurant":3,
"idRestaurant":3,
"restaurant":{
"idRestaurant":3,
"name":"paja rota",
"description":"La mejor parrilla de ramos mejia",
"socialNetwork":[
{
"idSocialNetworkRestaurant":3,
"idRestaurant":3,
"idTypeSocialNetwork":3,
"value":"twitter.com/Pajarota",
"type":{
"idTypeSocialNetwork":3,
"name":"Twitter",
"icon":"ic_twitter.png"
}
}
]
},
"idRangePriceBranch":3,
"RangePrice":{
"idRangePriceBranch":3,
"idBranchRestaurant":3,
"minimum":50,
"maximum":250
},
"name":"Ramos MEjia",
"photo":[
{
"idBranchPhoto":6,
"idBranchRestaurant":3,
"branch":null,
"idUser":0,
"user":null,
"photo":"EntradaPajaRota.jpg"
}
],
"cuisine":[
{
"idCousine":1,
"name":"Parrilla"
}
],
"menu":[
{
"idTypeMenu":4,
"name":"Mediodia",
"idBranchRestaurant":3,
"plates":null
},
{
"idTypeMenu":5,
"name":"Noche",
"idBranchRestaurant":3,
"plates":null
}
],
"filter":[
{
"idTypeFilter":2,
"name":"Family",
"icon":"family_ic.jpg"
}
],
"service":[
{
"idService":1,
"name":"Wifi"
},
{
"idService":4,
"name":"Carta de braille"
}
],
"timetable":[
{
"idTimetableBranch":20,
"idBranchRestaurant":3,
"idDay":5,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":5,
"name":"Friday"
}
},
{
"idTimetableBranch":21,
"idBranchRestaurant":3,
"idDay":5,
"openingHour":"19 hs",
"closingHour":"2 hs",
"day":{
"idDay":5,
"name":"Friday"
}
},
{
"idTimetableBranch":22,
"idBranchRestaurant":3,
"idDay":6,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":6,
"name":"Saturday"
}
},
{
"idTimetableBranch":23,
"idBranchRestaurant":3,
"idDay":6,
"openingHour":"19 hs",
"closingHour":"2 hs",
"day":{
"idDay":6,
"name":"Saturday"
}
},
{
"idTimetableBranch":24,
"idBranchRestaurant":3,
"idDay":7,
"openingHour":"12 hs",
"closingHour":"16 hs",
"day":{
"idDay":7,
"name":"Sunday"
}
}
],
"latitude":"12345678",
"longitude":"87654321",
"calification":null,
"averageFood":5.0,
"averageService":5.0,
"averageAmbience":5.0,
"averageCalification":5.0,
"promotion":[
]
},
{
"idBranchRestaurant":4,
"idRestaurant":4,
"restaurant":{
"idRestaurant":4,
"name":"la espiga",
"description":"el mejor lugar para comer pastas",
"socialNetwork":[
{
"idSocialNetworkRestaurant":4,
"idRestaurant":4,
"idTypeSocialNetwork":4,
"value":"instagram.com/laespiga",
"type":{
"idTypeSocialNetwork":4,
"name":"Instagram",
"icon":"ic_Instagram.png"
}
}
]
},
"idRangePriceBranch":4,
"RangePrice":{
"idRangePriceBranch":4,
"idBranchRestaurant":4,
"minimum":80,
"maximum":150
},
"name":"Almagro",
"photo":[
{
"idBranchPhoto":7,
"idBranchRestaurant":4,
"branch":null,
"idUser":0,
"user":null,
"photo":"EntradaLaEspiga.jpg"
}
],
"cuisine":[
{
"idCousine":4,
"name":"Pasta"
}
],
"menu":[
{
"idTypeMenu":6,
"name":"Tarde",
"idBranchRestaurant":4,
"plates":null
}
],
"filter":[
{
"idTypeFilter":1,
"name":"With Discount",
"icon":"discount_ic.jpg"
}
],
"service":[
{
"idService":1,
"name":"Wifi"
},
{
"idService":3,
"name":"Aire acondicionado"
}
],
"timetable":[
{
"idTimetableBranch":26,
"idBranchRestaurant":4,
"idDay":1,
"openingHour":"16 hs",
"closingHour":"19 hs",
"day":{
"idDay":1,
"name":"Monday"
}
},
{
"idTimetableBranch":27,
"idBranchRestaurant":4,
"idDay":2,
"openingHour":"16 hs",
"closingHour":"19 hs",
"day":{
"idDay":2,
"name":"Tuesday"
}
},
{
"idTimetableBranch":28,
"idBranchRestaurant":4,
"idDay":3,
"openingHour":"16 hs",
"closingHour":"19 hs",
"day":{
"idDay":3,
"name":"Wednesday"
}
},
{
"idTimetableBranch":29,
"idBranchRestaurant":4,
"idDay":4,
"openingHour":"16 hs",
"closingHour":"19 hs",
"day":{
"idDay":4,
"name":"Thursday"
}
},
{
"idTimetableBranch":30,
"idBranchRestaurant":4,
"idDay":5,
"openingHour":"16 hs",
"closingHour":"19 hs",
"day":{
"idDay":5,
"name":"Friday"
}
}
],
"latitude":"87657467",
"longitude":"45254567",
"calification":null,
"averageFood":5.0,
"averageService":5.0,
"averageAmbience":5.0,
"averageCalification":5.0,
"promotion":[
]
}
]
private String server = "http://appmorfando.azurewebsites.net/api/";
ParsingObjects parse = new ParsingObjects();
// Query for List Branches
private ArrayList<Branch> branches = new ArrayList<Branch>();
public ArrayList<Branch> getAllBranch(int limit, int offset){
new BranchGetAll().execute("branch/", limit + "", offset + "");
return branches;
}
private class BranchGetAll extends AsyncTask<String, Void, ArrayList<Branch>> {
protected void onPostExecute(ArrayList<Branch> datos) {
super.onPostExecute(datos);
branches = datos;
}
@Override
protected ArrayList<Branch> doInBackground(String... parametros) {
String url = parametros[0] + parametros[1] + "/" + parametros[2];
ArrayList<Branch> branchList = new ArrayList<Branch>();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(server + url)
.build();
String resultado;
try {
Response response = client.newCall(request).execute(); // Llamo al API Rest servicio1 en ejemplo.com
resultado = response.body().string();
} catch (IOException e) {
Log.d("Error",e.getMessage()); // Error de Network
return null;
}
try{
JSONArray jsonBranch = new JSONArray(resultado);
for (int i = 0; i < jsonBranch.length(); i++){
Branch b = new Branch();
JSONObject obj = jsonBranch.getJSONObject(i);
b = parse.branch(obj);
//Restaurant
JSONObject restoObj = obj.getJSONObject("restaurant");
b.restaurant = parse.restaurant(restoObj);
//Social Network
JSONArray socialNetwork = restoObj.getJSONArray("socialNetwork");
b.restaurant.social = parse.socialNetwork(socialNetwork);
// Range Price
JSONObject rangeObj = obj.getJSONObject("RangePrice");
b.range = parse.rangePrice(rangeObj);
//Photos
JSONArray photo = obj.getJSONArray("photo");
b.photo = parse.photoBranch(photo);
// Cuisine
JSONArray cuisine = obj.getJSONArray("cuisine");
b.cuisine = parse.cuisine(cuisine);
//Menu
JSONArray menu = obj.getJSONArray("menu");
b.menu = parse.menu(menu);
//Filter
JSONArray filter = obj.getJSONArray("filter");
b.filter = parse.filter(filter);
//Service
JSONArray service = obj.getJSONArray("service");
b.service = parse.service(service);
//Timetable
JSONArray timetable = obj.getJSONArray("timetable");
b.timetable = parse.timetable(timetable);
//Promotion
JSONArray promotion = obj.getJSONArray("promotion");
b.promotion = parse.promotion(promotion);
branchList.add(b);
}
return branchList;
}
catch (JSONException e) {
Log.d("error", e.getMessage());
return null;
}
}
}
public class lvRestaurantFrag extends Fragment implements AdapterView.OnItemClickListener{
MainActivity main;
ListView restaurantItems;
lvRestaurantAdapter adapter;
ParseQuery pq;
ArrayList<Branch> listBranches;
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle data) {
View toReturn;
toReturn = inflater.inflate(R.layout.frag_restaurant_lv, group, false);
main = (MainActivity)getActivity();
pq = new ParseQuery();
restaurantItems = (ListView) toReturn.findViewById(R.id.lvRestaurantItems);
// Here gets null so the adapter size is 0
listBranches = pq.getAllBranch(10,0);
adapter = new lvRestaurantAdapter(listBranches, main);
restaurantItems.setAdapter(adapter);
restaurantItems.setOnItemClickListener(this);
return toReturn;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int idBranch = adapter.getId(position);
main.BranchSelected(idBranch);
}
}
答案 0 :(得分:0)
这里有两件事:
你怎么知道你的asynctask没有到达onpostexecute?您附加的代码不会覆盖onpostexecute。让我们尝试实现该方法并执行一个简单的logcat或system.out来确认。
您的asynctask是从服务器获取Json对象并将它们打包到要返回的列表中。有很多方法可以解决此问题,例如服务器连接错误或json解析不正确。我会仔细测试你的连接和数据结构,以确保null是asynctask导致的。
最简单的测试方法之一是注释掉所有连接和json相关的代码,然后返回一个你知道可行的手动填充列表。这将有助于隔离失败的区域。