Bon, alors voici mon problème. J'essaye de créer une vue extensible de liste en récupérant des données du serveur. Mais mon application ne répond pas.L'application ne répond pas
Voici mon 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);
}
}
est ici code pour 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]);
}
}
}
}
}
Quelqu'un pourrait-il me aider où je me trompe? Dans la méthode onCreate, je récupère expandableListView et par la suite j'appelle la classe DataProvider pour la méthode getInfo();
Je soupçonne que la classe Dataprovider prend beaucoup de temps pour l'exécution. En outre, quelqu'un pourrait m'aider comment je peux déboguer le temps d'exécution pour le même.
Merci à l'avance
Savez-vous quelle ligne de code votre application est suspendue? Il existe plusieurs boucles for et while dans votre code, c'est un emplacement privilégié pour enquêter lorsque votre code se bloque. Essayez de mettre un System.out.println ("Salut"); à chaque étape dans les boucles pour voir où une boucle pourrait être hors de contrôle. Identifiez chaque boucle avec une chaîne différente. – bakoyaro
@bakoyaro Okay! J'ai donc essayé de faire ce que tu as suggéré. Et tout fonctionne bien jusqu'à la méthode doInBackground. Le système imprime la valeur hashMap renvoyée. Mais quand j'essaie d'attraper et d'imprimer résultat onPostExecute. Rien ne s'imprime. Peux-tu m'aider avec ça? –
Essayez de placer System.out.println() sur la première ligne de la méthode onPostExecute. Si le message n'est pas imprimé, le système n'appelle pas cette méthode. Une fois que vous êtes sûr que vous appelez la méthode, vous pouvez placer System.out.println() sur chaque ligne pour voir où le code est en cours d'exécution. – bakoyaro