Skip to content

Commit f4eb9e8

Browse files
committed
+):1.Fdv_StringRequest新增Get请求加入参数,Post请求相关方法封装,Fdv_StringRequest,Fdv_BaseRequest类也新增相关方法,用起来更加灵活
2.VolleyTestActivity新增Get,Post封装请求测试demo方法
1 parent 957ccff commit f4eb9e8

5 files changed

Lines changed: 320 additions & 3 deletions

File tree

Volley/src/main/java/com/chinaztt/fdv/Fdv_BaseRequest.java

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import com.android.volley.RequestQueue;
77
import com.android.volley.toolbox.Volley;
88

9+
import org.apache.http.protocol.HTTP;
10+
11+
import java.io.UnsupportedEncodingException;
12+
import java.net.URLEncoder;
13+
import java.util.Map;
14+
915
/**
1016
* 当前类注释:
1117
* 项目名:FastDev4Android
@@ -16,16 +22,71 @@
1622
* 公司:江苏中天科技软件技术有限公司
1723
*/
1824
public class Fdv_BaseRequest{
25+
private static final String TAG=Fdv_BaseRequest.class.toString();
26+
private static final String DEFAULT_PARAMS_ENCODING = "UTF-8";
1927
protected static RequestQueue requestQueue;
2028
private Context mContext;
2129
protected Fdv_BaseRequest(Context pContext){
2230
this.mContext=pContext;
31+
requestQueue=Fdv_Volley.getInstace(mContext);
2332
}
2433
/**
2534
* 请求加入到Volley Request请求队列中
2635
* @param request
2736
*/
28-
protected void addRequest(Request request){
29-
Fdv_Volley.getInstace(mContext).add(request);
37+
protected void addRequest(Request request){
38+
this.addRequest(request,null);
39+
}
40+
41+
/**
42+
* 请求和请求参数 加入到Volley Request请求队列中
43+
* @param request
44+
* @param params
45+
*/
46+
protected void addRequest(Request request,Map<String,String> params){
47+
//请求中添加 请求参数
48+
request.setParams(params);
49+
requestQueue.add(request);
50+
}
51+
52+
/**
53+
* 根据请求地址和请求参数进行拼接成新的请求地址
54+
* @param url 请求服务器地址
55+
* @param params 请求参数
56+
* @return 拼接过后的地址
57+
*/
58+
protected String createGetUrlWithParams(String url,Map<String,String> params){
59+
if(params!=null){
60+
StringBuffer stringBuffer=new StringBuffer(url);
61+
if(!url.contains("?")){
62+
stringBuffer.append('?');
63+
}
64+
for(Map.Entry<String,String> entry:params.entrySet()){
65+
String key=entry.getKey().toString();
66+
String value=null;
67+
if(entry.getValue()==null){
68+
value="";
69+
}else {
70+
value=entry.getValue().toString();
71+
}
72+
stringBuffer.append(key);
73+
stringBuffer.append("=");
74+
try {
75+
Fdv_Log.d(TAG,"get获取数据的key:"+key);
76+
Fdv_Log.d(TAG,"get获取数据的value:"+value);
77+
value= URLEncoder.encode(value, DEFAULT_PARAMS_ENCODING);
78+
Fdv_Log.d(TAG,"get编码后value:"+value);
79+
stringBuffer.append(value);
80+
} catch (UnsupportedEncodingException e) {
81+
e.printStackTrace();
82+
}
83+
stringBuffer.append('&');
84+
}
85+
//删除最后一个'&'
86+
stringBuffer.deleteCharAt(stringBuffer.length()-1);
87+
url=stringBuffer.toString();
88+
}
89+
Fdv_Log.d(TAG,"get请求地址url:"+url);
90+
return url;
3091
}
3192
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.chinaztt.fdv;
2+
3+
/**
4+
* 当前类注释:重写系统日志管理类--Volley专用日志管理类
5+
* 使用方法:还是和平时Log.v(key,value)这样使用,需要导入当前类,该类会打印比系统更多的日志信息,
6+
* 例如:类名称,当前运行的方法,行数,和日志信息
7+
* 项目名:FastDev4Android
8+
* 包名:com.chinaztt.fda.utils
9+
* 作者:江清清 on 15/10/22 09:35
10+
* 邮箱:jiangqqlmj@163.com
11+
* QQ: 781931404
12+
* 公司:江苏中天科技软件技术有限公司
13+
*/
14+
public class Fdv_Log {
15+
public static boolean mIsShow=true;
16+
17+
/**
18+
* 设置是否打开log日志开关
19+
* @param pIsShow
20+
*/
21+
public static void setShow(boolean pIsShow)
22+
{
23+
mIsShow=pIsShow;
24+
}
25+
26+
/**
27+
* 根据tag打印相关v信息
28+
* @param tag
29+
* @param msg
30+
*/
31+
public static void v(String tag,String msg)
32+
{
33+
if(mIsShow){
34+
StackTraceElement ste = new Throwable().getStackTrace()[1];
35+
String traceInfo = ste.getClassName() + "::";
36+
traceInfo += ste.getMethodName();
37+
traceInfo += "@" + ste.getLineNumber() + ">>>";
38+
android.util.Log.v(tag, traceInfo+msg);}
39+
}
40+
41+
/**
42+
* 根据tag打印v信息,包括Throwable的信息
43+
* * @param tag
44+
* @param msg
45+
* @param tr
46+
*/
47+
public static void v(String tag,String msg,Throwable tr)
48+
{
49+
if(mIsShow){
50+
android.util.Log.v(tag, msg, tr);
51+
}
52+
}
53+
54+
55+
/**
56+
* 根据tag打印输出debug信息
57+
* @param tag
58+
* @param msg
59+
*/
60+
public static void d(String tag,String msg)
61+
{
62+
if(mIsShow){
63+
StackTraceElement ste = new Throwable().getStackTrace()[1];
64+
String traceInfo = ste.getClassName() + "::";
65+
traceInfo += ste.getMethodName();
66+
traceInfo += "@" + ste.getLineNumber() + ">>>";
67+
android.util.Log.d(tag, traceInfo+msg);
68+
}}
69+
70+
/**
71+
* 根据tag打印输出debug信息 包括Throwable的信息
72+
* * @param tag
73+
* @param msg
74+
* @param tr
75+
*/
76+
public static void d(String tag,String msg,Throwable tr)
77+
{
78+
if(mIsShow){
79+
android.util.Log.d(tag, msg, tr);
80+
}}
81+
82+
/**
83+
* 根据tag打印输出info的信息
84+
* * @param tag
85+
* @param msg
86+
*/
87+
public static void i(String tag,String msg)
88+
{
89+
if(mIsShow){
90+
StackTraceElement ste = new Throwable().getStackTrace()[1];
91+
String traceInfo = ste.getClassName() + "::";
92+
traceInfo += ste.getMethodName();
93+
traceInfo += "@" + ste.getLineNumber() + ">>>";
94+
android.util.Log.i(tag, traceInfo+msg);
95+
}}
96+
97+
/**
98+
* 根据tag打印输出info信息 包括Throwable的信息
99+
* @param tag
100+
* @param msg
101+
* @param tr
102+
*/
103+
public static void i(String tag,String msg,Throwable tr)
104+
{
105+
if(mIsShow){
106+
android.util.Log.i(tag, msg, tr);
107+
}}
108+
109+
/**
110+
* 根据tag打印输出error信息
111+
* @param tag
112+
* @param msg
113+
*/
114+
public static void e(String tag,String msg)
115+
{
116+
if(mIsShow){
117+
StackTraceElement ste = new Throwable().getStackTrace()[1];
118+
String traceInfo = ste.getClassName() + "::";
119+
traceInfo += ste.getMethodName();
120+
traceInfo += "@" + ste.getLineNumber() + ">>>";
121+
android.util.Log.e(tag, traceInfo+msg);
122+
}}
123+
124+
/**
125+
* 根据tag打印输出的error信息 包括Throwable的信息
126+
* @param tag
127+
* @param msg
128+
* @param tr
129+
*/
130+
public static void e(String tag,String msg,Throwable tr)
131+
{
132+
if(mIsShow){
133+
android.util.Log.e(tag, msg, tr);
134+
}}
135+
}

Volley/src/main/java/com/chinaztt/fdv/Fdv_StringRequest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.android.volley.VolleyError;
88
import com.android.volley.toolbox.StringRequest;
99

10+
import java.util.Map;
11+
1012
/**
1113
* 当前类注释:Volley 字符串、文本数据请求封装类
1214
* 项目名:FastDev4Android
@@ -20,6 +22,11 @@ public class Fdv_StringRequest<T> extends Fdv_BaseRequest{
2022
public Fdv_StringRequest(Context pContext){
2123
super(pContext);
2224
}
25+
/**
26+
* 普通文本等信息 Get请求 无参数,或者get请求的参数直接拼接在URL上面
27+
* @param url 请求的地址
28+
* @param listener 数据请求返回接口回调
29+
*/
2330
public void get(String url, final Fdv_CallBackListener<T> listener){
2431
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
2532
@Override
@@ -38,4 +45,56 @@ public void onErrorResponse(VolleyError error) {
3845
});
3946
addRequest(stringRequest);
4047
}
48+
49+
/**
50+
* 普通文本等信息 Get请求 携带请求参数
51+
* @param url 请求地址
52+
* @param listener 数据请求接口回调
53+
* @param params 请求的参数
54+
*/
55+
public void get(String url, final Fdv_CallBackListener<T> listener,Map<String,String> params){
56+
//进行在url地址上面拼接请求参数
57+
url=createGetUrlWithParams(url,params);
58+
StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
59+
@Override
60+
public void onResponse(String response) {
61+
if(listener!=null){
62+
listener.onSuccessResponse((T) response);
63+
}
64+
}
65+
}, new Response.ErrorListener() {
66+
@Override
67+
public void onErrorResponse(VolleyError error) {
68+
if(listener!=null){
69+
listener.onErrorResponse(error);
70+
}
71+
}
72+
});
73+
addRequest(stringRequest);
74+
}
75+
76+
/**
77+
* 根据地址和请求参数 发送POST请求
78+
* @param url 地址服务器地址
79+
* @param listener 数据加载回调接口
80+
* @param params 请求参数
81+
*/
82+
public void post(String url, final Fdv_CallBackListener<T> listener,Map<String,String> params){
83+
StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
84+
@Override
85+
public void onResponse(String response) {
86+
if(listener!=null){
87+
listener.onSuccessResponse((T) response);
88+
}
89+
}
90+
}, new Response.ErrorListener() {
91+
@Override
92+
public void onErrorResponse(VolleyError error) {
93+
if(listener!=null){
94+
listener.onErrorResponse(error);
95+
}
96+
}
97+
});
98+
addRequest(stringRequest,params);
99+
}
41100
}

0 commit comments

Comments
 (0)