Volley怎么用才帅

buildgride添加

 implementation 'com.mcxiaoke.volley:library:1.0.19'

主页

package com.demo.volley.myvolley.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.demo.volley.myvolley.R;
import com.demo.volley.myvolley.app.MyApplication;
import com.demo.volley.myvolley.volley.PictureCache;
import com.demo.volley.myvolley.volley.VolleyInterface;
import com.demo.volley.myvolley.volley.VolleyRequest;

import java.util.HashMap;


public class MainActivity extends AppCompatActivity {

    private ImageView mIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
//        initGet();
//        initPost();
    }

    private void initView() {
        mIv = findViewById(R.id.iv);
        String url = "https://www.baidu.com/img/bd_logo1.png";
        ImageLoader imageLoader = new ImageLoader(MyApplication.getHttpQueue(), new PictureCache());
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(mIv, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background);
        imageLoader.get(url, listener);
    }

    private void initPost() {
        String url = "https://www.so.com/s?src=lm&ls=sm2330543&lm_extend=ctype:31&q=ds";
        HashMap<String, String> map = new HashMap<>();
        map.put("src", "lm");
        map.put("ls", "sm2330543");
        map.put("lm_extend", "ctype:31");
        map.put("q", "ds");
        VolleyRequest.RequestPost(url, "post_volley", this, map, new VolleyInterface(this) {
            @Override
            public void MySuccessful(String response) {
                Log.e("1123", response);
            }

            @Override
            public void MyError(VolleyError ve) {
                Log.e("1123", ve.toString());
            }
        });
    }

    private void initGet() {
        String url = "https://www.baidu.com/";
        VolleyRequest.RequestGet(url, "get_volley", this, new VolleyInterface(this) {
            @Override
            public void MySuccessful(String response) {
                Log.e("1123", response);
            }

            @Override
            public void MyError(VolleyError ve) {
                Log.e("1123", ve.toString());
            }
        });
    }
}

主页布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

工具类

package com.demo.volley.myvolley.volley;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

public class PictureCache implements ImageLoader.ImageCache {

    private final LruCache<String, Bitmap> mCache;
    private int max = 10 * 1024 * 1024;

    public PictureCache() {
        mCache = new LruCache<String, Bitmap>(max) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }

    @Override
    public Bitmap getBitmap(String url) {
        return mCache.get(url);
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        mCache.put(url, bitmap);
    }
}
package com.demo.volley.myvolley.volley;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.demo.volley.myvolley.app.MyApplication;

public abstract class VolleyInterface {
    private StringRequest mStringRequest;
    private Response.ErrorListener mErrorListener;
    private Response.Listener<String> mStringListener;
    private Context mc;

    public VolleyInterface(Context mc) {
        this.mc = mc;
        this.mStringListener = LoadingListener();
        this.mErrorListener = ErrorListener();
    }

    public Response.Listener<String> LoadingListener() {
        mStringListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                MySuccessful(response);
            }
        };
        return mStringListener;
    }

    public abstract void MySuccessful(String response);

    public Response.ErrorListener ErrorListener() {
        mErrorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                MyError(error);
            }
        };
        return mErrorListener;
    }

    public abstract void MyError(VolleyError ve);


}
package com.demo.volley.myvolley.volley;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.StringRequest;
import com.demo.volley.myvolley.app.MyApplication;

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

public class VolleyRequest {

    private static StringRequest mStringRequest;

    public static void RequestGet(String url, String tag, Context mc, VolleyInterface vi) {
        MyApplication.getHttpQueue().cancelAll(tag);
        mStringRequest = new StringRequest(Request.Method.GET, url, vi.LoadingListener(), vi.ErrorListener());
        mStringRequest.setTag(tag);
        MyApplication.getHttpQueue().add(mStringRequest);
        MyApplication.getHttpQueue().start();
    }

    public static void RequestPost(String url, String tag, Context mc, final HashMap<String, String> map, VolleyInterface vi) {
        MyApplication.getHttpQueue().cancelAll(tag);
        mStringRequest = new StringRequest(Request.Method.POST, url, vi.LoadingListener(), vi.ErrorListener()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        mStringRequest.setTag(tag);
        MyApplication.getHttpQueue().add(mStringRequest);
        MyApplication.getHttpQueue().start();
    }
}
发布了75 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sinat_40387150/article/details/94326561