0

我想在移动活动时显示加载过程 这是我的 java 文件,我必须在其中放置函数以在加载数据时处理我的应用程序加载

public class tehni extends ListActivity {
TextView txt1;
private static String link_url = "http://192.168.32.1/pln/android/berita/tehnik1.php?kode=";
private static final String AR_ID = "id";
private static final String AR_JUDUL = "judul";
private static final String AR_CONTENT = "content";
JSONArray artikel = null;
ArrayList<HashMap<String, String>> daftar_artikel = new ArrayList<HashMap<String, String>>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tehni);
    JSONParserberita jParser = new JSONParserberita();
    JSONObject json = jParser.AmbilJson(link_url + user_name);
    try {
        artikel = json.getJSONArray("artikel");
        for (int i = 0; i < artikel.length(); i++) {
            JSONObject ar = artikel.getJSONObject(i);
            String id = ar.getString(AR_ID);
            String judul = "ID LAPORAN   =" + ar.getString(AR_JUDUL);
            String content = "STATUS="
                    + ar.getString(AR_CONTENT).substring(0, 6);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(AR_ID, id);
            map.put(AR_JUDUL, judul);
            map.put(AR_CONTENT, content);
            daftar_artikel.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    this.adapter_listview();
}

}

4

3 回答 3

1

AsyncTask是 Android 提供的一个抽象类,可以帮助我们正确使用 UI 线程。此类允许我们执行长/后台操作并在 UI 线程上显示其结果,而无需操作线程。

尝试使用 AsynchTask

public class tehni extends ListActivity {
TextView txt1;
private static String link_url = "http://192.168.32.1/pln/android/berita/tehnik1.php?kode=";
private static final String AR_ID = "id";
private static final String AR_JUDUL = "judul";
private static final String AR_CONTENT = "content";
JSONArray artikel = null;
ArrayList<HashMap<String, String>> daftar_artikel = new ArrayList<HashMap<String, String>>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tehni);
    new task ().execute();

}
class task extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDlg.setMessage("Loading...");
        pDlg.setCancelable(false);
        pDlg.setTitle(getResources().getString(R.string.app_name));
        pDlg.setCancelable(false);
        pDlg.show();
    }

    @Override
    protected String doInBackground(String... params) {
         JSONParserberita jParser = new JSONParserberita();
JSONObject json = jParser.AmbilJson(link_url + user_name);
try {
    artikel = json.getJSONArray("artikel");
    for (int i = 0; i < artikel.length(); i++) {
        JSONObject ar = artikel.getJSONObject(i);
        String id = ar.getString(AR_ID);
        String judul = "ID LAPORAN   =" + ar.getString(AR_JUDUL);
        String content = "STATUS="
                + ar.getString(AR_CONTENT).substring(0, 6);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(AR_ID, id);
        map.put(AR_JUDUL, judul);
        map.put(AR_CONTENT, content);
        daftar_artikel.add(map);
    }
} catch (JSONException e) {
    e.printStackTrace();
}
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
                    pDlg.dismiss();
                 this.adapter_listview();

    }
}
于 2013-10-18T06:18:47.277 回答
0

尝试AsyncTask用于 Web 服务调用。请参阅更多详细信息 developer.android.com/reference/android/os/AsyncTask.html

public class tehni extends ListActivity {
TextView txt1;
private static String link_url = "http://192.168.32.1/pln/android/berita/tehnik1.php?kode=";
private static final String AR_ID = "id";
private static final String AR_JUDUL = "judul";
private static final String AR_CONTENT = "content";
JSONArray artikel = null;
ArrayList<HashMap<String, String>> daftar_artikel = new ArrayList<HashMap<String, String>>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tehni);
    new ServiceCallTask().execute();
    this.adapter_listview();
}
class ServiceCallTask extends AsyncTask<String, String, String>
{

    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog=new ProgressDialog(tehni.this, android.R.style.Theme_Translucent_NoTitleBar);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        try {

            JSONParserberita jParser = new JSONParserberita();
            JSONObject json = jParser.AmbilJson(link_url + user_name);
                artikel = json.getJSONArray("artikel");
                for (int i = 0; i < artikel.length(); i++) {
                    JSONObject ar = artikel.getJSONObject(i);
                    String id = ar.getString(AR_ID);
                    String judul = "ID LAPORAN   =" + ar.getString(AR_JUDUL);
                    String content = "STATUS="
                            + ar.getString(AR_CONTENT).substring(0, 6);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(AR_ID, id);
                    map.put(AR_JUDUL, judul);
                    map.put(AR_CONTENT, content);
                    daftar_artikel.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(progressDialog!=null)
            progressDialog.dismiss();
        this.adapter_listview();
    }
}


}
于 2013-10-18T06:13:52.630 回答
0

如果在 UI 线程上运行 HTTP 请求,您将收到NetworkOnMainTheadException,我将假设您的请求在jParser.AmbilJson().

因此,最好的方法是使用 AsyncTask 之类的方法onPreExecute并将onPostExecute结果显示在ProgressBar中。

于 2013-10-18T06:23:03.930 回答