I'm trying to solve this problem for weeks now but I can't solve it so I really hope to find help here.
I'm creating an app which is working much with the webserver so there are many queries to do. In my Fragment, named "tab_profile" I call 2 queries to the API and many other to load images on my custom ListView.
So my problem is that this fragment needs 1-2 second on loading (well connection) and even crashing on bad connection e.g. 3G.
The code is somehow looking like this (Short version of course):
public class tab_profile extends Fragment implements AdapterView.OnItemClickListener {
private String imagepath = null;
private ImageView imageview = null;
View rootView = null;
private ArrayList<PollItem> arrayPolls;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab_profile, container, false);
final Button button_follower = (Button) rootView.findViewById(R.id.button_follower);
final Button button_following = (Button) rootView.findViewById(R.id.button_following);
final TextView text_username = (TextView) rootView.findViewById(R.id.text_username);
imageview = (ImageView) rootView.findViewById(R.id.img_own_pp);
final Session session = new Session(getActivity());
final Network network = new Network(getActivity());
text_username.setText(session.getValue("username"));
if(session.getValue("username").length() < 3) {
getActivity().finish();
return null;
}
JSONArray arr;
String res = network.POST("{\"token\":\"" + session.getValue("token") + "\"}", "profile.php");
try {
JSONObject obj = new JSONObject(res);
arr = obj.getJSONArray("data");
if (!obj.get("status").equals("false")) {
button_follower.setText("Follower\n" + arr.getJSONObject(0).getString("follower"));
button_following.setText("Following\n" + arr.getJSONObject(0).getString("following"));
if (arr.getJSONObject(0).getString("pp").equals("true")) {
network.setImageViewImage(imageview, "usermedia/pp/" + session.getValue("id"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
arrayPolls = new ArrayList<>();
getList();
//listeners here
return rootView;
}
Then the second main function which is called "getList()":
public void getList() {
Network netwok = new Network(getActivity());
Session session = new Session(getActivity());
String resp = netwok.POST("{\"token\":\""+session.getValue("token")+"\","+
"\"get\":\"true\""+"}","mypoll.php");
try {
JSONObject obj = new JSONObject(resp);
JSONArray arr = obj.getJSONArray("data");
if(obj.get("status").equals("false")) {
return;
}
arrayPolls.clear();
for(int i=0; i<arr.length(); i++) {
JSONObject ot = arr.getJSONObject(i);
PollItem tmp = new PollItem(ot.getInt("ID"),ot.getInt("userid"),
ot.getString("description"),ot.getString("username"));
arrayPolls.add(tmp);
}
}
catch (JSONException e) {
e.printStackTrace();
}
ListView list_poll_item = (ListView) rootView.findViewById(R.id.list_friend_poll);
PollOverviewItemAdapter adapter = new PollOverviewItemAdapter(getActivity(), arrayPolls, tab_profile.this, imageview);
list_poll_item.setAdapter(adapter);
list_poll_item.setOnItemClickListener(this);
}
And my queries are sent by this Async. function:
private class POST_REQUEST extends AsyncTask<String, Integer, String>{
protected String doInBackground(String... params) {
String line;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
ArrayList<NameValuePair> postParameters;
postParameters = new ArrayList<>();
postParameters.add(new BasicNameValuePair("json", query));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
and the last function:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
So I think those were the important parts of my code. I really hope to find a solution for this problem. And I've tried many times to load first my UI and then later the data from server but it was the same. I tried this by using "onStart()".
Thanks in advance... :-)
I think I found my own solution. I just used my asnyc function wrong. If I do everything in onPostExecute() the app works well. Before, I did the main part in doinbackground callback so it somehow got stuck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With