本文介绍了如何为排球库创建单独的类,并从另一个活动中调用排球的所有方法并获得响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个单独的类,在其中定义所有有关排球的知识在另一项活动中,我们直接传递URL,CONTEXT和获取响应...

how to create a separate class in which define all about volley and in another activity we directly pass URL,CONTEXT and Get Response...

推荐答案

首先创建回调接口以在Activity中获取结果

First create callback interface to get result in Activity

public interface IResult {
    public void notifySuccess(String requestType,JSONObject response);
    public void notifyError(String requestType,VolleyError error);
}

使用排球功能创建一个单独的类,以通过与活动的接口响应结果

Create a separate class with volley function to response the result through interface to activity

public class VolleyService {

    IResult mResultCallback = null;
    Context mContext;

    VolleyService(IResult resultCallback, Context context){
        mResultCallback = resultCallback;
        mContext = context;
    }


    public void postDataVolley(final String requestType, String url,JSONObject sendObj){
        try {
            RequestQueue queue = Volley.newRequestQueue(mContext);

            JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null)
                        mResultCallback.notifySuccess(requestType,response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(mResultCallback != null)
                        mResultCallback.notifyError(requestType,error);
                }
            });

            queue.add(jsonObj);

        }catch(Exception e){

        }
    }

    public void getDataVolley(final String requestType, String url){
        try {
            RequestQueue queue = Volley.newRequestQueue(mContext);

            JsonObjectRequest jsonObj = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(mResultCallback != null)
                        mResultCallback.notifySuccess(requestType, response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    if(mResultCallback != null)
                        mResultCallback.notifyError(requestType, error);
                }
            });

            queue.add(jsonObj);

        }catch(Exception e){

        }
    }
} 

然后将回调接口初始化为主要活动

Then initialize callback interface into main activity

    mResultCallback = new IResult() {
        @Override
        public void notifySuccess(String requestType,JSONObject response) {
            Log.d(TAG, "Volley requester " + requestType);
            Log.d(TAG, "Volley JSON post" + response);
        }

        @Override
        public void notifyError(String requestType,VolleyError error) {
            Log.d(TAG, "Volley requester " + requestType);
            Log.d(TAG, "Volley JSON post" + "That didn't work!");
        }
    };

现在创建VolleyService类的对象,并将其传递给上下文和回调接口

Now create object of VolleyService class and pass it context and callback interface

mVolleyService = new VolleyService(mResultCallback,this);

现在调用Volley方法进行发布或获取数据还传递requestType,该请求类型用于在将结果返回到主要活动中时识别服务请求者

Now call the Volley method for post or get data also pass requestType which is to identify the service requester when getting result back into main activity

    mVolleyService.getDataVolley("GETCALL","http://192.168.1.150/datatest/get/data");
    JSONObject sendObj = null;

    try {
        sendObj = new JSONObject("{'Test':'Test'}");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    mVolleyService.postDataVolley("POSTCALL", "http://192.168.1.150/datatest/post/data", sendObj);

最终MainActivity

Final MainActivity

public class MainActivity extends AppCompatActivity {
    private String TAG = "MainActivity";
    IResult mResultCallback = null;
    VolleyService mVolleyService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initVolleyCallback();
        mVolleyService = new VolleyService(mResultCallback,this);
        mVolleyService.getDataVolley("GETCALL","http://192.168.1.150/datatest/get/data");
        JSONObject sendObj = null;

        try {
            sendObj = new JSONObject("{'Test':'Test'}");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mVolleyService.postDataVolley("POSTCALL", "http://192.168.1.150/datatest/post/data", sendObj);
    }

    void initVolleyCallback(){
        mResultCallback = new IResult() {
            @Override
            public void notifySuccess(String requestType,JSONObject response) {
                Log.d(TAG, "Volley requester " + requestType);
                Log.d(TAG, "Volley JSON post" + response);
            }

            @Override
            public void notifyError(String requestType,VolleyError error) {
                Log.d(TAG, "Volley requester " + requestType);
                Log.d(TAG, "Volley JSON post" + "That didn't work!");
            }
        };
    }

}

在以下链接中找到整个项目

Find the whole project at following link

https://github.com/PatilRohit/VolleyCallback

这篇关于如何为排球库创建单独的类,并从另一个活动中调用排球的所有方法并获得响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:33