Страницы

Поиск по вопросам

вторник, 9 июля 2019 г.

Android приложение перестало работать

Написал приложение, которое принимает JSON с сервера и результат выводит в RecyclerView. Изначально все работало корректно, но через некоторое время стало зависать на ProgressBar в Android 5.0. На эмуляторе Android studio все работает отлично (эмулятор Android 7.1.1). Зависает в фоновом потоке при десериализации JSON
MainActivity.java
public class MainActivity extends AppCompatActivity {
Context context; ProgressBar progressBar; RecyclerView filmView; Activity main;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); main = this; context=this; setContentView(R.layout.activity_main); filmView = (RecyclerView) findViewById(R.id.FilmsList); progressBar = (ProgressBar) findViewById(R.id.progressBar); progressBar.setVisibility(ProgressBar.VISIBLE);
MyThread myThread = new MyThread(); Thread thread = new Thread(myThread); thread.start(); }
/** * Класс для обработки Json */ public class MyThread extends Thread{ private final long date=System.currentTimeMillis()/1000L; private final String FILMS_URL ="http://example.com" private String resultJson; private JSONObject jsonObject; private JSONArray js; Adapter adapter; private Films[] films; InputStream inputStream; @Override public void run(){ try { URL url = new URL(FILMS_URL); URLConnection http = url.openConnection(); inputStream = http.getInputStream(); http.connect(); StringBuilder buffer = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); } catch (IOException e) { e.printStackTrace(); }finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { jsonObject = new JSONObject(resultJson); js = jsonObject.getJSONArray("results"); films =new Gson().fromJson(js.toString(), Films[].class); } catch (JSONException e) { e.printStackTrace(); } filmView.post(new Runnable() { @Override public void run() {
filmView.addItemDecoration(new SpacesItemDecoration(50)); filmView.setHasFixedSize(true); adapter = new Adapter(main, context, films); LinearLayoutManager llm = new LinearLayoutManager(main); llm.setOrientation(LinearLayoutManager.VERTICAL); filmView.setLayoutManager(llm); filmView.setAdapter(adapter);
/** * Обработка нажатий */ filmView.addOnItemTouchListener( new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) { Intent intent = new Intent(MainActivity.this, FilmDetails.class); String filmString; GsonBuilder gsonBuilder=new GsonBuilder(); gsonBuilder.registerTypeAdapter(Films.class, new MyTypeAdapter()); gsonBuilder.setPrettyPrinting(); Gson gson=gsonBuilder.create(); filmString =gson.toJson(films[position]); intent.putExtra("films", filmString); //передаём объект как json-строку startActivity(intent); } }) ); } }); } }
public void onProgressClick(View view){ Toast toast=Toast.makeText(this, "loading...", Toast.LENGTH_SHORT); toast.setGravity(Gravity.BOTTOM, 0, 0); toast.show(); } }
в логи пишет это
01-13 13:45:31.464 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 53.907ms 01-13 13:45:33.422 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 5.432ms 01-13 13:45:35.849 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 9256(454KB) AllocSpace objects, 3(47KB) LOS objects, 21% free, 2017KB/2MB, paused 4.327ms total 312.016ms 01-13 13:45:35.876 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 24.495ms 01-13 13:45:35.953 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 34.178ms 01-13 13:45:36.939 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 5.087ms 01-13 13:45:38.017 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 90.718ms 01-13 13:45:39.033 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 106.362ms 01-13 13:45:39.123 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 5725(315KB) AllocSpace objects, 1(16KB) LOS objects, 13% free, 2MB/2MB, paused 3.789ms total 499.241ms 01-13 13:45:39.565 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 140.064ms 01-13 13:45:39.734 1353-1365/com.example.sharafievp.myfilm I/art: Background partial concurrent mark sweep GC freed 8075(378KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 2MB/3MB, paused 2.541ms total 374.222ms 01-13 13:45:39.756 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 22.256ms 01-13 13:45:41.457 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 10.842ms 01-13 13:45:42.026 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 21.661ms 01-13 13:45:42.306 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 26452(1138KB) AllocSpace objects, 0(0B) LOS objects, 38% free, 2MB/3MB, paused 3.173ms total 350.458ms 01-13 13:45:42.340 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 32.739ms 01-13 13:45:42.478 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 45.701ms 01-13 13:45:42.941 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 7.528ms 01-13 13:45:43.441 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 9.983ms 01-13 13:45:43.967 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 15.002ms 01-13 13:45:44.441 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 5.679ms 01-13 13:45:45.017 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 78.054ms 01-13 13:45:45.227 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 30991(1278KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 2MB/3MB, paused 4.014ms total 530.324ms 01-13 13:45:45.248 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 20.060ms 01-13 13:45:46.952 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 6.040ms 01-13 13:45:47.519 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 24262(1051KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 2MB/3MB, paused 28.907ms total 413.226ms 01-13 13:45:47.522 1353-1364/com.example.sharafievp.myfilm I/art: WaitForGcToComplete blocked for 276.630ms for cause HeapTrim 01-13 13:45:47.621 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 93.925ms 01-13 13:45:47.952 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 6.986ms 01-13 13:45:48.445 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 5.285ms 01-13 13:45:49.451 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 7.622ms 01-13 13:45:49.977 1353-1360/com.example.sharafievp.myfilm W/art: Suspending all threads took: 33.428ms 01-13 13:45:50.156 1353-1365/com.example.sharafievp.myfilm W/art: Suspending all threads took: 5.254ms 01-13 13:45:50.407 1353-1365/com.example.sharafievp.myfilm I/art: Background sticky concurrent mark sweep GC freed 27212(1124KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 2MB/3MB, paused 11.058ms total 474.047ms
на english форумах пишут что проблема связана не с кодом, а с Android 5.0. Пользуюсь библиотекой Gson


Ответ

В общем ни одна из библиотек обработки JSON у меня не работает на Android 5.0. Написал собственный класс для обработки, используя стандартную библиотеку org.json. Теперь приложение не зависает.

Комментариев нет:

Отправить комментарий