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(); 
} 

}

回答

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(); 
    } 
} 


} 
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(); 

    } 
} 
0

考慮,如果運行在UI線程上一個HTTP請求,您將收到一個NetworkOnMainTheadException,我會假設你的要求是內jParser.AmbilJson()

所以,最好的方法是使用AsyncTask的方法,如onPreExecuteonPostExecuteProgressBar中顯示結果。