接口回调:
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();