2016-04-29 61 views
0

我需要使用webservice獲取圖像,這是rest.And大多數教程是關於點擊按鈕,以觸發process.I需要實現這樣一種方式,當活動打開圖像有無需觸發或點擊按鈕即可立即加載。我只需要一個源或想法。 任何幫助將不勝感激。android Restful GET操作

回答

1

實現一個Asynctask並將其稱爲onCreate。

下面是如何做到這一點的例子;

public class MainActivity extends ListActivity { 
. 
. 
. 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    . 
    . 
    . 

    // Calling async task to get json 
    new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     ServiceHandler sh = new ServiceHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

     Log.d("Response: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String id = c.getString(TAG_ID); 
        String name = c.getString(TAG_NAME); 
        String email = c.getString(TAG_EMAIL); 
        String address = c.getString(TAG_ADDRESS); 
        String gender = c.getString(TAG_GENDER); 

        // Phone node is JSON Object 
        JSONObject phone = c.getJSONObject(TAG_PHONE); 
        String mobile = phone.getString(TAG_PHONE_MOBILE); 
        String home = phone.getString(TAG_PHONE_HOME); 
        String office = phone.getString(TAG_PHONE_OFFICE); 

        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_ID, id); 
        contact.put(TAG_NAME, name); 
        contact.put(TAG_EMAIL, email); 
        contact.put(TAG_PHONE_MOBILE, mobile); 

        // adding contact to contact list 
        contactList.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       MainActivity.this, contactList, 
       R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL, 
         TAG_PHONE_MOBILE }, new int[] { R.id.name, 
         R.id.email, R.id.mobile }); 

     setListAdapter(adapter); 
    } 

} 

}

正如你所看到的,你只需要調用的AsyncTask(新GetContacts()執行();)的onCreate然後它會爲您設計的。

參考文獻:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

+0

謝謝你的回答。我會試試看。 –

+0

非常感謝您的關注。 –

+0

不客氣 – Ricardo

0

onCreate()方法Activity中寫入您的加載圖像的代碼。所以用戶不需要觸發或點擊按鈕或其他東西。

+0

感謝您answer.I會嘗試一下。 –

+0

每次重新創建活動(包括方向更改)時,都會調用onCreate()。查看android生命週期頁面http://developer.android.com/training/basics/activity-lifecycle/index.html。也許包括一些緩存類型的機制,所以你不必每次都下載圖像。如果你只是學習一個教程,但是理解它是一件好事,那麼可能會過度。 –

+1

您可以使用Image Downloder來爲您緩存圖像。請檢查https://github.com/nostra13/Android-Universal-Image-Loader –