【问题标题】:Run app and then download json运行应用程序,然后下载 json
【发布时间】:2016-12-10 18:30:31
【问题描述】:

我正在制作一个 Android 应用程序,它使用来自下载的 JSON 文件(由 GSON 解析)的内容。现在,setContentView() 之后的所有事情都发生在onCreate() 方法中,我认为这对性能不是很好。 首先我想加载和显示我的应用程序的内容(布局、视图),然后然后从 URL 下载 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();
}

【问题讨论】:

  • 显示相关代码。
  • 您应该将您的网络代码移动到AsyncTask
  • 所以我的根类应该extendsAsyncTask?
  • 不一定。它可以是一个内部类。在文档中阅读:AsyncTask,这是一个教程:AndroidBackgroundProcessing

标签: android json performance http gson


【解决方案1】:

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

【讨论】:

  • 谢谢,我试试看。
猜你喜欢
  • 2015-05-18
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2013-07-04
  • 1970-01-01
  • 2019-05-25
相关资源
最近更新 更多