运行应用程序,然后下载json

时间:2016-12-10 18:30:32

标签: android json performance http gson

我正在制作一个Android应用程序,它使用下载的JSON文件中的内容(由GSON解析)。现在,onCreate()之后setContentView()方法中的所有内容都发生了,我觉得这对性能不是很好。 首先我想加载并显示我的应用内容(布局,视图),然后然后从网址下载JSON。我怎样才能实现它?好的例子是Facebook的Android应用程序。

来源:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xyz);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    if(isNetworkAvailable()) {
        try {
            URL urlLiczba = new URL("https://xyz/");
            InputStreamReader readerLiczba = new InputStreamReader(urlLiczba.openStream(/*SK*/));

            Gson gson = new Gson();
            LiczbaImprez liczbaImprez = gson.fromJson(readerLiczba, LiczbaImprez.class);

            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout layout = null;
            ImageView logo = null;
            TextView nazwa = null;
            TextView czasMiejsce = null;
            String czasMiejsceStr;
            TextView opis = null;
            LinearLayout lista = (LinearLayout) findViewById(R.id.listaImprez);

            for (int i = 0; i < Integer.parseInt(liczbaImprez.liczba); i++) {
                URL url = new URL("https://xyz/dir/" + i);
                InputStreamReader reader = new InputStreamReader(url.openStream());
                JsonReader jsonReader = new JsonReader(reader);
                jsonReader.setLenient(true);
                Imprezy imprezy = gson.fromJson(jsonReader, Imprezy.class);

                layout = (LinearLayout) inflater.inflate(R.layout.item_imprezy, null);
                logo = (ImageView) layout.findViewById(R.id.ikonaImprezy);
                nazwa = (TextView) layout.findViewById(R.id.nazwaImprezy);
                czasMiejsce = (TextView) layout.findViewById(R.id.czasMiejsce);
                opis = (TextView) layout.findViewById(R.id.opisImprezy);

                switch (imprezy.typ) {
                    case "element1":
                        logo.setImageResource(R.drawable.logo_of_element1);
                        break;
                    case "element2":
                        logo.setImageResource(R.drawable.logo_of_element2);
                        break;
                    default:
                        break;
                }
                nazwa.setText(imprezy.nazwa);
                czasMiejsceStr = imprezy.data + " - " + imprezy.miejsce;
                czasMiejsce.setText(czasMiejsceStr);
                opis.setText(imprezy.opis);
                lista.addView(layout);
            }
        } catch (MalformedURLException e) {
            Toast.makeText(getApplicationContext(), "Something went wrong :/.", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {

        }
    }
    else{
        Toast.makeText(getApplicationContext(), "No connection!", Toast.LENGTH_LONG).show();
    }
}
    private class LiczbaImprez {
    String liczba;
}

private class Imprezy{
    String typ;
    String nazwa;
    String miejsce;
    String data;
    String opis;
}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

1 个答案:

答案 0 :(得分:0)

Android调用主线程中的所有方法。您应该将方法分成不同的线程。对于新主题,您应该这样做:new Thread(new Runnable{ your code; }).start(); click for details。这个例子。另一个例子是使用Asynctasks。有了这个,你可以在后台进行一些操作,并在主线程中绘制你的用户界面。为此,只需将您的课程扩展到AsyncTaskclick for details。 最后,为了解析数据,您可以使用一些库,例如loopjvolley by googleretrofit by square。这些库可以帮助您传输数据,甚至将API转换为Java接口。