好吧,這是我的問題。我試圖通過從服務器獲取數據來創建一個可擴展的列表視圖。但我的應用程序沒有響應。應用程序沒有響應
這裏是我的代碼 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);
}
}
這裏的代碼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]);
}
}
}
}
}
有人能幫助我,我要去的地方錯了嗎? 在onCreate方法中,我獲取expandableListView,然後調用DataProvider類獲取方法getInfo();
我懷疑Dataprovider類需要很長時間才能執行。 此外,有人可以幫助我如何調試相同的執行時間。
由於提前
你知道你的應用掛在哪行代碼嗎?代碼中有幾個for循環和while循環,這是調查代碼掛起時的主要位置。嘗試把一個System.out。的println( 「你好」);在循環的每一步中查看循環可能失控的位置。用不同的字符串標識每個循環。 – bakoyaro
@bakoyaro好的!所以我試着做你的建議。所有的工作都很好,直到doInBackground方法。系統打印返回的hashMap值。但是,當我嘗試捕獲並在onPostExecute打印結果。沒有打印。你能幫我解決這個問題嗎? –
嘗試在onPostExecute方法的第一行放置System.out.println()。如果消息未打印,則系統不會調用該方法。一旦你確定你正在調用該方法,你可以在每行上放一個System.out.println()來查看代碼的執行位置。 – bakoyaro