如何在android中获取API响应后调用函数?

时间:2017-01-14 21:21:09

标签: android multithreading api

我使用了可扩展列表视图。

我必须在Api回复后才打电话给Handler。将其置于apiCalls()内是在启动应用后报告空白活动。所有这些都在MainActivity中完成。可以在函数 package com.example.expandablelistview; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ExpandableListView; import android.widget.Toast; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.ion.Ion; import java.util.ArrayList; import java.util.LinkedHashMap; public class MainActivity extends AppCompatActivity { DBManager dbManager = new DBManager(MainActivity.this); private LinkedHashMap<String, GroupInfo> subjects = new LinkedHashMap<String, GroupInfo>(); private ArrayList<GroupInfo> deptList = new ArrayList<GroupInfo>(); private CustomAdapter listAdapter; private ExpandableListView simpleExpandableListView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // add data for displaying in expandable list view //get reference of the ExpandableListView simpleExpandableListView = (ExpandableListView) findViewById(R.id.simpleExpandableListView); // create the adapter by passing your ArrayList data listAdapter = new CustomAdapter(MainActivity.this, deptList); // attach the adapter to the expandable list view simpleExpandableListView.setAdapter(listAdapter); // expandAll(); if (Utility.isNetworkAvailable(this)) { apiCall(); } else { Toast.makeText(this, "Internet not available", Toast.LENGTH_SHORT).show(); } if (dbManager.checkTablesInitialsed() == true) { loadData(); } } private void apiCall() { JsonParser jsonParser = new JsonParser(this); final ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("Searching..."); progressDialog.setCancelable(false); progressDialog.show(); Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { progressDialog.dismiss(); } }; handler.postDelayed(runnable, 8000); if (dbManager.checkTablesInitialsed() == false) { Ion.with(this).load(Constant.URL_CLASS).setTimeout(8000).asString() .setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String response) { if (e != null) { Toast.makeText(MainActivity.this, "Sorry!!Problem establishing connection with the server- class", Toast.LENGTH_SHORT).show(); Log.e("Exception.class..", e + ""); } else { try { Log.e("RESPONSE ..class", response); JsonParser.getDataFromServer_class(response); // Toast.makeText(Search.this, "Count" + wordBean.getCount(), Toast.LENGTH_SHORT).show(); } catch (Exception parseException) { Toast.makeText(MainActivity.this, "Error hitting the API", Toast.LENGTH_SHORT).show(); Log.e("Search...class ", parseException.toString()); } } } }); Ion.with(this).load(Constant.URL_SECTION).setTimeout(8000).asString() .setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String response) { if (e != null) { Toast.makeText(MainActivity.this, "Sorry!!Problem establishing connection with the server- SECTION", Toast.LENGTH_SHORT).show(); Log.e("Exception...SECTION", e + ""); } else { try { Log.e("RESPONSE .SECTION.", response); JsonParser.getDataFromServer_section(response); // Toast.makeText(Search.this, "Count" + wordBean.getCount(), Toast.LENGTH_SHORT).show(); } catch (Exception parseException) { Toast.makeText(MainActivity.this, "Error hitting the API SECTION", Toast.LENGTH_SHORT).show(); Log.e("Search...SECTION", parseException.toString()); } } } }); Ion.with(this).load(Constant.URL_SUBJECT).setTimeout(8000).asString() .setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String response) { if (e != null) { Toast.makeText(MainActivity.this, "Sorry!!Problem establishing connection with the server- SUBJECT", Toast.LENGTH_SHORT).show(); Log.e("Exception..SUBJECT", e + ""); } else { try { Log.e("RESPONSE ..SUBJECT", response); JsonParser.getDataFromServer_subject(response); // Toast.makeText(Search.this, "Count" + wordBean.getCount(), Toast.LENGTH_SHORT).show(); } catch (Exception parseException) { Toast.makeText(MainActivity.this, "Error hitting the API SUBJECT", Toast.LENGTH_SHORT).show(); Log.e("Search...SUBJECT ", parseException.toString()); } } } }); Ion.with(this).load(Constant.URL_TIMETABLE).setTimeout(8000).asString() .setCallback(new FutureCallback<String>() { @Override public void onCompleted(Exception e, String response) { if (e != null) { Toast.makeText(MainActivity.this, "Sorry!!Problem establishing connection with the server- TIMETABLE", Toast.LENGTH_SHORT).show(); Log.e("Exception..TIMETABLE.", e + ""); } else { try { Log.e("RESPONSE ..TIMETABLE", response); JsonParser.getDataFromServer_timetable(response); progressDialog.dismiss(); // Toast.makeText(Search.this, "Count" + wordBean.getCount(), Toast.LENGTH_SHORT).show(); } catch (Exception parseException) { Toast.makeText(MainActivity.this, "Error hitting the API TIMETABLE", Toast.LENGTH_SHORT).show(); Log.e("Search.. TIMETABLE ", parseException.toString()); } } } }); } } //method to expand all groups private void expandAll() { int count = listAdapter.getGroupCount(); for (int i = 0; i < count; i++) { simpleExpandableListView.expandGroup(i); } } //method to collapse all groups private void collapseAll() { int count = listAdapter.getGroupCount(); for (int i = 0; i < count; i++) { simpleExpandableListView.collapseGroup(i); } } private void loadData() { int i, k = 1, size = 0, j = 0; String ar[] = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; ArrayList<WordBean> arrayList; WordBean wordBean; String str; for (i = 2; i <= 7; i++) { arrayList = dbManager.getTimetable(i); j = 0; k = 1; size = arrayList.size(); // Log.e("classs", "" + size); while (j < size) { wordBean = arrayList.get(j); if (wordBean.getLectureNo().equals(k + "")) { str = " Class:" + wordBean.getClassName() + " Sec:" + wordBean.getSectionName() + " Sub:" + wordBean.getSubName(); j++; } else { str = "FREE--"; } addProduct(ar[i], "Lecture " + k + " :" + str); k++; } while (k <= 8) { addProduct(ar[i], "Lecture " + k + " :" + "FREE--"); k++; } } } //here we maintain our products in various departments private int addProduct(String department, String product) { int groupPosition = 0; //check the hash map if the group already exists GroupInfo headerInfo = subjects.get(department); //add the group if doesn't exists if (headerInfo == null) { headerInfo = new GroupInfo(); headerInfo.setName(department); subjects.put(department, headerInfo); deptList.add(headerInfo); } //get the children for the group ArrayList<ChildInfo> productList = headerInfo.getProductList(); //size of the children list int listSize = productList.size(); //add to the counter listSize++; //create a new child and add that to the group ChildInfo detailInfo = new ChildInfo(); detailInfo.setSequence(String.valueOf(listSize)); detailInfo.setName(product); productList.add(detailInfo); headerInfo.setProductList(productList); //find the group position inside the list groupPosition = deptList.indexOf(headerInfo); return groupPosition; } } enter code here 中找到API调用。将Handler置于api调用中会在加载活动时再次出错。

这是我的代码:

Object

0 个答案:

没有答案