Volley的基本使用

Volley的基本使用

導入jar包

P1

導入成功java

P2

源碼

GET請求下載地址:http://download.csdn.net/detail/q4878802/9053969android

POST請求下載地址:http://download.csdn.net/detail/q4878802/9054111markdown

說明

應用比較靈活因此沒有封裝,能夠根據本身的需求去進行封裝網絡

這裏是啓動Activity之後就開始輪詢請求網絡,退出之後取消請求。app

添加網絡權限

<!-- 添加網絡權限 -->
<uses-permission android:name="android.permission.INTERNET" />

GET請求

package com.kongqw.kqwvolleydemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;


public class MainActivity extends Activity {

    private RequestQueue mQueue;
    private StringRequest stringRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 建立一個RequestQueue對象
        mQueue = Volley.newRequestQueue(this);

        // GET請求
        stringRequest = new StringRequest("http://www.baidu.com",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("TAG", response);
                        Toast.makeText(MainActivity.this, "response = " + response, Toast.LENGTH_SHORT).show();
                        mQueue.add(stringRequest);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
                Toast.makeText(MainActivity.this, "error = " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        mQueue.add(stringRequest);
    }

    @Override
    protected void onStop() {
        mQueue.stop();
        super.onStop();
    }
}

POST請求

package com.kongqw.kqwvolleydemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends Activity {

    private RequestQueue mQueue;
    private StringRequest stringRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 建立一個RequestQueue對象
        mQueue = Volley.newRequestQueue(this);

        // POST請求
        stringRequest = new StringRequest(Request.Method.POST, "http://www.baidu.com",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("TAG", response);
                        Toast.makeText(MainActivity.this, "response = " + response, Toast.LENGTH_SHORT).show();
                        mQueue.add(stringRequest);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
                Toast.makeText(MainActivity.this, "error = " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("params1", "value1");
                map.put("params2", "value2");
                return map;
            }
        };

        mQueue.add(stringRequest);
    }

    @Override
    protected void onStop() {
        mQueue.stop();
        super.onStop();
    }
}

請求數據ide

P3

相關文章
相關標籤/搜索