Volley介紹JSon 和提交字串引數的封裝(StringRequest和Gson)

dingcheng998發表於2016-11-17

介面回撥:


public interface StringVolleyCallback {
    //StringFromNet()和StringFromAndPutNet()方法呼叫
    public void getStringFromVolley(String s);
    //StringFromAndPutNet()方法呼叫
    public void putStringFromVolley(Map<String, String> map);
    //對json的字串進行解析
    public void getJsonFromJsonString(Object o);
    //輸出異常資訊
    public void getJsonErrorResponse(String s);
}

方法類:


/**
 * 
 * 從網路獲取字串資料
 */

public class DataOfString {
    private Context context;
    private String url;
    private RequestQueue mQueue;
    private Class<?>  mClass;
    private int method;
    private Gson gson;
    //介面回撥步驟2:定義介面成員變數
    private StringVolleyCallback stringVolleyCallback;
    //介面回撥步驟3:例項化介面
    public void setVolleyCallBck(StringVolleyCallback stringVolleyCallback) {
        this.stringVolleyCallback = stringVolleyCallback;
    }

    public DataOfString(Context context, String url,int method,Class<?>  mClass){
        this.context = context;
        this.url = url;
        this.method = method;
        this.mClass = mClass;
        mQueue = Volley.newRequestQueue(context);

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                return new Date(json.getAsJsonPrimitive().getAsLong());
            }
        });
       gson = builder.create();
    }

    public DataOfString(View view,String url,int method,Class<?>  mClass){
        this(view.getContext(),url,method,mClass);
    }

    public void StringFromNet() {
        StringRequest stringRequest = new StringRequest(method,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                //介面回撥步驟4:實現介面回撥方法
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.getStringFromVolley(s);
                    stringVolleyCallback.getJsonFromJsonString(gson.fromJson(s,mClass));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //列印錯誤資訊
                Log.e("請求錯誤資訊:", volleyError.getMessage(), volleyError);
                if (stringVolleyCallback !=null){
                   stringVolleyCallback.getJsonErrorResponse(volleyError.getMessage());
                }
            }
        }){
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                //獲取session
                new ConnectViaSession().getSession(response);
                return super.parseNetworkResponse(response);
            }
        };

        mQueue.add(stringRequest);
    }

    public void StringFromAndPutNet() {
        StringRequest stringRequest = new StringRequest(method, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.getStringFromVolley(s);
                    stringVolleyCallback.getJsonFromJsonString(gson.fromJson(s,mClass));
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                //列印錯誤資訊
                Log.e("TAG", volleyError.getMessage(), volleyError);
            }
        }){
            //傳送session
             @Override
             public Map<String, String> getHeaders() throws AuthFailureError {
                 //傳送session
                 String sesseionid = new ConnectViaSession().GetSession();
                 if (!sesseionid.equals("")) {
                     HashMap<String, String> headers = new HashMap<String, String>();
                     headers.put("cookie", sesseionid);
                     return headers;
                 }else {
                     return super.getHeaders();
              }
           }

            //給伺服器提交引數
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                if (stringVolleyCallback !=null){
                    stringVolleyCallback.putStringFromVolley(map);
                    return map;
                }
                return map;
            }
        };
        mQueue.add(stringRequest);
    }
}

使用:

DataOfString json = new DataOfString(getApplicationContext(),url, Request.Method.POST,loginPojo.class);
        json.setVolleyCallBck(new StringVolleyCallback() {
            @Override
            public void getStringFromVolley(String s) {
                //System.out.println(s);
            }

            @Override
            public void putStringFromVolley(Map<String, String> map) {
       
            }

            @Override
            public void getJsonFromJsonString(Object o) {
                 
                 }
            }

            @Override
            public void getJsonErrorResponse(String s) {

            }
        });
        json.StringFromAndPutNet();


相關文章