Android學習筆記:Andorid網路請求架構Volley的使用(上)

來源:互聯網
上載者:User

標籤:android開發   volley架構   網路請求   

   Volley架構是Google I/O 2013大會上發布的。Volley是Google針對Android平台上的網路通訊庫而誕生,該架構能使網路通訊更快,更簡單,更健壯。Volley的特點有:

Ⅰ:通訊更簡單更快捷

ll:Get,Post網路請求及網狀圖像進行高效非同步處理

III:可以對多個網路請求進行優先順序排序以級多層級取消操作

IV:網路請求緩衝及與Activity生命週期進行聯動,在Activity銷毀的時候可以對網路請求進行取消操作,防止記憶體泄露。

 Volley的使用很簡單:

1,下載Volley的jar包或是原始碼,github地址:https://github.com/stormzhang/AndroidVolley

2,擷取RequestQueue,一般我們是放到Application中進行初始化操作

3,執行個體化一個Request對象(StringRequest,JsonObjectRequest,JsonArrayRequest)4,將Request加入RequestQueue即可。
首先學習的是最基礎的StringRequest:首頁面布局檔案:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="10dp" >
    <Button
        android:id="@+id/get_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="GET請求"
        android:textSize="16sp" 
        android:layout_marginTop="15dp"/>
    <Button
        android:id="@+id/post_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="POST請求"
        android:textSize="16sp" />
</LinearLayout>
-------------請求操作------------------------public class MainActivity extends Activity implements OnClickListener {
private Button getBtn, postBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBtn = (Button) findViewById(R.id.get_btn);
postBtn = (Button) findViewById(R.id.post_btn);
getBtn.setOnClickListener(this);
postBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_btn:// GET請求擷取資料
getStringRequest();
break;
case R.id.post_btn:// POST請求擷取資料
postStringequest();
break;
}
}


private void getStringRequest() {// 用GET方法使用字串資料請求StringRequest
String url = "http://apis.juhe.cn/mobile/get?phone=134****4320&key=d6099881640aed5e0bacc058cfd4357b";//key值是在彙總資料上申請的api
StringRequest strRequest = new StringRequest(Method.GET, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {// 請求成功
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {// 請求失敗
Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
});
strRequest.setTag("getLDM");
MyApplication.getRequestQueues().add(strRequest);
}

private void postStringequest() {// 用POST方法使用字串資料請求StringRequest
String url = "http://apis.juhe.cn/mobile/get?";
StringRequest postRequest = new StringRequest(Method.POST, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {

@Override
public void onErrorResponse(VolleyError arg0) {                      Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {// getparams方法是Volley中使用POST請求資料時傳遞參數資訊
Map<String, String> map = new HashMap<String, String>();
map.put("phone", "134****4320");
map.put("key", "d6099881640aed5e0bacc058cfd4357b");
return map;
}
};
postRequest.setTag("postLDM");
MyApplication.getRequestQueues().add(postRequest);
}
}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android學習筆記:Andorid網路請求架構Volley的使用(上)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.