volley的简单使用

2015/8/19问题集

volley的简单使用

MyApplication中的代码:主要是建立一个queue对列。并给出一个调用方法

package com.zsz.develop.volleydemo;

import android.app.Application;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * Created by shengzhong on 2015/8/19.
 */
public class MyApplication extends Application {

public static RequestQueue queue;

@Override
public void onCreate() {
    super.onCreate();
    queue= Volley.newRequestQueue(getApplicationContext());
}

public static RequestQueue getHttpQueue(){
    return queue;
}

}

=

MainActivity中的代码。调用和实现volley的post和get方法

package com.zsz.develop.volleydemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends ActionBarActivity {

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

private void volley_Post() {
    String url="http://apis.juhe.cn/mobile/get?";
    StringRequest request=new StringRequest(Request.Method.POST,url,new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
        }
    },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(MainActivity.this,"网络请求失败",Toast.LENGTH_LONG).show();
        }
    }){
        //与get最大的不同就是在这后面要添加getParams的方法来传递参数
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String,String> map=new HashMap<>();
            map.put("phone","13616008640");
            map.put("key","a8d4ff7a12cabef64ac6fe7871baf073");
            //返回map就好了
            return map;
        }
    };
    request.setTag("abcPost");
    MyApplication.getHttpQueue().add(request);
}

private void volley_Get() {
    String url="http://apis.juhe.cn/mobile/get?phone=13616008640&key=a8d4ff7a12cabef64ac6fe7871baf073";
    StringRequest request=new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {

            Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
        }
    },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(MainActivity.this,"网络请求失败",Toast.LENGTH_LONG).show();
        }
    });
    //添加标签,当取消这个请求的时候可以取消这个标签
    request.setTag("abcGET");
    //将我们的请求添加到我们的对列里面
    MyApplication.getHttpQueue().add(request);

}


}