我在Android应用程序中从服务器中提取数据时遇到问题。加载数据需要很长时间,请注意它只有5行,我想在我的情况下,因为我在每个循环中拉动用户图片。我不知道更好的方法,有什么建议吗?
以下是我在后台函数中执行此操作的代码:
protected ArrayList<User> doInBackground(Object... Void) {
String response = performGetCall(SERVER_ADDRESS + "FetchAll.php", null);
User user = null;
ArrayList<User> listUsers = new ArrayList<User>();
try {
JSONArray allUsersData = new JSONArray(response);
//list = ArrayUtil.convert(allUsersData);
//Log.d("List", list.toString());
for (int i=0, l=allUsersData.length(); i<l; i++) {
JSONObject userData = allUsersData.getJSONObject(i);
String imageName = userData.getString("imageName");
Bitmap image = null;
if (imageName != null) {
String imagePath = SERVER_ADDRESS + "pictuers/" + imageName;
HttpURLConnection conn = null;
try {
URL imageUrl = new URL(imagePath);
conn = (HttpURLConnection) imageUrl.openConnection();
conn.setReadTimeout(CONNECTION_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
image = BitmapFactory.decodeStream((InputStream) conn.getContent(), null, null);
} catch (IOException e) {
Log.d("Image get exception:",e.toString());
e.printStackTrace();
image = null;
}
}else {
imageName = "";
}
user = new User(userData.getInt("user_id"), userData.getString("name"), userData.getString("userName"), userData.getString("email"),userData.getString("gender"),userData.getString("password"),userData.getString("phone"), imageName,image);
listUsers.add(user);
// Log.d("Rturned User: ",returnedUser.email);
//Log.d("Response", userData.toString());
}
return listUsers;
} catch (JSONException e) {
e.printStackTrace();
}
return listUsers;
}