如何为凌空图书馆制作单独的课程并从另一个活动中调用所有的抽象方法并得到回应?

如何创build一个单独的类,其中定义所有关于凌空,并在另一个活动中,我们直接通过URL,CONTEXT和获取响应…

Solutions Collecting From Web of "如何为凌空图书馆制作单独的课程并从另一个活动中调用所有的抽象方法并得到回应?"

首先创buildcallback接口,以获得结果在活动

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

使用抽象函数创build一个单独的类,以通过接口将结果响应到活动

 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){ } } } 

然后将callback接口初始化为主活动

  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!"); } }; 

现在创buildVolleyService类的对象并传递它的上下文和callback接口

 mVolleyService = new VolleyService(mResultCallback,this); 

现在调用Volley方法进行发布,或者让数据也传递requestType,这是在返回主活动的时候识别服务请求者

  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); 

最后的主要活动

 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整个项目

https://github.com/PatilRohit/VolleyCallback

你实际上错过了上面的VolleyService类中的一个参数。 你需要包括,它是,… JsonObjectRequest jsonObj = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener(){/ …. /} null是参数应该包括否则它给出错误

创build监听器(因为它们是接口,它们不能被实例化,但是它们可以作为实现接口的匿名类来实现)。 并将这些实例作为parameter passing给Request(StringRequest,JsonObjectRequest或ImageRequest)。

 public class MainActivity extends Activity { private static final String URI = ""; // This is like BroadcastReceiver instantiation private Listener<JSONObject> listenerResponse = new Listener<JSONObject>() { @Override public void onResponse(JSONObject arg0) { // Do what you want with response } }; private ErrorListener listenerError = new ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { // Do what you want with error } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } 

}

接下来,创build一个具有请求的类,并将此侦听器传递给此类的请求方法,就这些了。 我没有解释这部分,这与在任何教程中创build一个请求对象是一样的。但是你可以自定义这个类。 您可以创build单身RequestQueue来检查优先级,或者将身体http身体参数设置为此方法的参数。

 public class NetworkHandler { public static void requestJSON(Context context, String url, Listener<JSONObject> listenerResponse, ErrorListener listenerError) { JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, listenerResponse, listenerError); Volley.newRequestQueue(context).add(jsonRequest); } 

}

JsonParserVolley.java

(一个单独的类,我们将得到答复)

 public class JsonParserVolley { final String contentType = "application/json; charset=utf-8"; String JsonURL = "Your URL"; Context context; RequestQueue requestQueue; String jsonresponse; private Map<String, String> header; public JsonParserVolley(Context context) { this.context = context; requestQueue = Volley.newRequestQueue(context); header = new HashMap<>(); } public void addHeader(String key, String value) { header.put(key, value); } public void executeRequest(int method, final VolleyCallback callback) { StringRequest stringRequest = new StringRequest(method, JsonURL, new Response.Listener<String>() { @Override public void onResponse(String response) { jsonresponse = response; Log.e("RES", " res::" + jsonresponse); callback.getResponse(jsonresponse); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { return header; } } ; requestQueue.add(stringRequest); } public interface VolleyCallback { public void getResponse(String response); } 

}

MainActivity.java(用onCreate方法写的代码片段)

 final JsonParserVolley jsonParserVolley = new JsonParserVolley(this); jsonParserVolley.addHeader("Authorization", "Your value"); jsonParserVolley.executeRequest(Request.Method.GET, new JsonParserVolley.VolleyCallback() { @Override public void getResponse(String response) { jObject=response; Log.d("VOLLEY","RES"+jObject); parser(); } } ); 

parser()是获取的json响应用于绑定活动的组件的方法。