Android中Volley的接口回调机制传回数据的示例

步骤1:定义接口类

package com.example.cuboo.myapplication.DataFromNetApi;

/**
 * Created by cuboo on 2016/10/27.
 * 接口回调步骤1:定义监听接口
 */

public interface VolleyCallBck {
     public void getStringFromVolley(String s);
}



步骤2:实现接口类

package com.example.cuboo.myapplication.DataFromNetApi;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

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.Map;

/**
 * Created by cuboo on 2016/10/26.
 * 从网络获取字符串数据
 */

public class DataOfString {
    private Context context;
    private String url;
    private RequestQueue mQueue;
    //接口回调步骤2:定义接口成员变量
    private VolleyCallBck volleyCallBck;
    //接口回调步骤3:实例化接口
    public void setVolleyCallBck(VolleyCallBck volleyCallBck) {
        this.volleyCallBck = volleyCallBck;
    }

    public DataOfString(Context context, String url){
        this.context = context;
        this.url = url;
        mQueue = Volley.newRequestQueue(context);
    }
    public DataOfString(View view,String url){
        this(view.getContext(),url);
    }

    public void StringFromNet() {
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url, new Response.Listener() {
            @Override
            public void onResponse(String s) {
                //接口回调步骤4:实现接口回调方法
                if (volleyCallBck !=null){
                    volleyCallBck.getStringFromVolley(s);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //打印错误信息
                Log.e("TAG", volleyError.getMessage(), volleyError);
            }
        });
        mQueue.add(stringRequest);
    }

    public void StringFromAndPutNet(final Map map) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener() {
            @Override
            public void onResponse(String s) {
                if (volleyCallBck !=null){
                    volleyCallBck.getStringFromVolley(s);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //打印错误信息
                Log.e("TAG", volleyError.getMessage(), volleyError);
            }
        }){  //给服务器提交参数
            @Override
            protected Map getParams() throws AuthFailureError {
                return map;
            }
        };
        mQueue.add(stringRequest);
    }
}



步骤3:调用回调方法

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private ImageView img;
    private NetworkImageView net_img;

    //private String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = "https://www.baidu.com";
        tv = (TextView) findViewById(R.id.textview);
        DataOfString dataOfString = new DataOfString(getApplicationContext(), url);
        
        //接口回调步骤5:获得回调的数据
        dataOfString.setVolleyCallBck(new VolleyCallBck() {
            @Override
            public void getStringFromVolley(String s) {
                System.out.println("测试了" + s);
                tv.setText(s + "");
            }
        });
     //接口回调步骤6:实现回调传递参数。
        dataOfString.StringFromNet();
}}
 
  









你可能感兴趣的:(Android)