I used to think that the code either works or not. But it was not there)

I am writing an application, such as a store, on the emulator everything works clearly, there are never glitches. When I test on mobile via wifi, it works 3-5-7 times, like when, it has its own atmosphere. When I test on mobile via the mobile Internet, the necessary elements are not loaded at all.

Wryly this part is loaded, where different views are formed depending on the email and are selected on the server.

  StringRequest request = new StringRequest(Request.Method.POST, select_all, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonObject = new JSONObject(response); JSONArray jsonArray = jsonObject.getJSONArray("image_info_table"); int sum=0; for(int i=0;i<jsonArray.length();i++){ JSONObject info = jsonArray.getJSONObject(i); Item item=new Item(); item.setItem_name_Cart(info.getString("name")); item.setImage(info.getString("path")); item.setQuantity(info.getString("quantity")); item.setPrice(info.getString("price")); item.setSpinner_size(info.getString("size")); array.add(item); } } catch (JSONException e) { e.printStackTrace(); adapter.notifyDataSetChanged(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> parameters = new HashMap<String, String>(); parameters.put("email", email.getText().toString()); return parameters; } 

JSON catches, but this subsequent transfer and incomprehensible work puts me in a stupor. Maybe due to the fact that I have Meizu on Flyme and something the Chinese have namuduli there, or some kind of overloaded part of the mysql php .

What can be the purely theoretical reason, on the emulator it works well, maybe because the computer has more resources or what?

  • There are no null variable checks when creating objects. This code should not work. - Vanyamba Electronics

2 answers 2

Even the code is strange

  try { JSONObject jsonObject = new JSONObject(response); ... } catch (JSONException e) { e.printStackTrace(); adapter.notifyDataSetChanged(); } 

I understand that the adapter is responsible for displaying the array in a ListView

And if you translate your code into Russian, it sounds like this: parse JSON, and if an error occurs during the parse, then update the adapter

It is necessary to take out adapter.notifyDataSetChanged(); from the catch

 try { JSONObject jsonObject = new JSONObject(response); ... } catch (JSONException e) { e.printStackTrace(); } adapter.notifyDataSetChanged(); 
  • Thank you, it was here that sin was. - Romik romikromik

I will not go into the subtleties of the TC code (colleagues have already indicated them). I’ll say something else: the passion for anonymous students / classes doesn’t do much good

Everything should have its own measure, an anonymous listener / class / lambda has a clear meaning when the body is short and simple - no question.

But as soon as the class body or its complexity begins to exceed a certain limit, it is necessary to carry it out at least to the inner class, and even the public outer class. As a result:

  1. Improves code readability
  2. It is easier to control the life cycle of an object (since it must be explicitly created) - therefore, the possibility of leakage is significantly reduced.

Where is the limit when to abandon anonymous class / lambda? Honestly I do not know. Roughly speaking, there is more screen: that's all, Basset-karapuziki - we are leaving to a named class, like that.

I did not try to theoretically substantiate this position (this is not very interesting to me) - strictly practice, strictly sleepless nights alone with a debugger and a fierce customer :)

  • Thank you very much, I certainly understand that it is very far, so far, to the best traditions of writing code, well, we will go to this) - Romik romikromik