Okay so here's my problem. I am trying to create an expandable list view by fetching data from server. But my app is not responding.
Here's my code MainActivity.java
public class MainActivity extends AppCompatActivity {
HashMap<String, List<String>> Movies_category;
List<String> Movies_list;
ExpandableListView Exp_list;
MoviesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
Movies_category = DataProvider.getInfo();
Movies_list = new ArrayList<String>(Movies_category.keySet());
adapter = new MoviesAdapter(this, Movies_category, Movies_list);
Exp_list.setAdapter(adapter);
}
}
Here's code for DataProvider.java
public class DataProvider {
private static HashMap<String,List<String>> MoviesDetails;
public static HashMap<String, List<String>> getInfo(){
String[] SecondArray = {"One","Two","Three"};
String[] Category = {"Action_Movies","Romantic_Movies","Horror_Movies"};
new JSONTask().execute("http://pruthvi.co/tests/ems/machine-query.php");
JSONTask json = new JSONTask();
try {
//json.get();
MoviesDetails = json.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return MoviesDetails;
}
public static class JSONTask extends AsyncTask<String,String,HashMap<String,List<String>>> {
@Override
protected HashMap<String,List<String>> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection)url.openConnection();
connection.connect();
// connecting to the url
//Reading the data in bytes stream
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
//Reading the data by creating a buffer
StringBuffer buffer = new StringBuffer();
String line="";
while((line = reader.readLine())!= null){
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
List<String> list = new ArrayList<String>();
JSONArray array = parentObject.getJSONArray("machine-array");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("name")+"\n");
}
HashMap<String,List<String>> map = new HashMap<String,List<String>>();
for(int ij=0; ij<list.size();ij++){
map.put(list.get(ij),list);
}
return map;
//return list.toArray(new String[list.size()]);
// setting text view from the url
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally{
if(connection !=null) {
connection.disconnect();
}
try {
if (reader != null)
{
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(HashMap<String,List<String>> result) {
super.onPostExecute(result);
HashMap<String, List<String>> MoviesDetails = new HashMap<String, List<String>>();
String[] SecondArray = {"One","Two","Three","four","five","six"};
String[] resultArray = (String[]) result.values().toArray();
for(int j=0; j<resultArray.length;j++) {
MoviesDetails.put(resultArray[j], new ArrayList<String>());
}
for(int k=0;k<resultArray.length;k++)
{
for (int i=0;i<SecondArray.length; i++) {
MoviesDetails.get(resultArray[k]).add(SecondArray[i]);
}
}
}
}
}
Could someone help me out where I am going wrong? In onCreate Method, I am fetching expandableListView and thereafter I am calling DataProvider class for method getInfo();
I suspect Dataprovider class is taking a long time for execution. Also, Someone could help me how I can debug the execution time for the same.
Thanks in Advance