一:对Http不了解的请看

Android Http请求框架一:Get 和 Post 请求

 

二、正文

1、xUtils 下载地址

    github 下载地址  : https://github.com/wyouflf/xUtils

 

2、关于网络请求的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.jike.shanglv.NetAndJson;
 
import java.io.File;
 
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
 
public class HttpUtil {
 
    String result = "" ;
 
    /**
     * Get请求  异步的
     * @param url  服务器地址
     * @param userkey
     * @param str
     * @param sign 签名
     * @return
     */
    public String xutilsGet( String url , String userkey , String str , String sign  ){
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("userkey", userkey );
        params.addQueryStringParameter("str", str );
        params.addQueryStringParameter("sign", sign );
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000 10); //设置超时时间   10s
        http.send(HttpRequest.HttpMethod.GET,
                url ,
                new RequestCallBack<String>(){
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
 
            }
 
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                result = responseInfo.result.toString() ;
            }
 
            @Override
            public void onStart() {
            }
 
            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
 
        return result ;
    }
 
    /**
     * Post请求 异步的
     * @param url
     * @param userkey
     * @param str
     * @param sign
     * @return
     */
    public String xutilsPost( String url , String userkey , String str , String sign ){
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("userkey", userkey );
        params.addQueryStringParameter("str", str );
        params.addQueryStringParameter("sign", sign );
 
        // 只包含字符串参数时默认使用BodyParamsEntity,
        // 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。
        //params.addBodyParameter("name", "value");
 
        // 加入文件参数后默认使用MultipartEntity("multipart/form-data"),
        // 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。
        // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
        // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
        // 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
 
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000 10); //设置超时时间   10s 
        http.send(HttpRequest.HttpMethod.POST ,
                url ,
                params,
                new RequestCallBack<String>() {
 
            @Override
            public void onStart() {
            }
 
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }
 
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                result = responseInfo.result.toString() ;
            }
 
            @Override
            public void onFailure(HttpException error, String msg) {
 
            }
        });
 
        return result ;
    }
 
    /**
     * 带上传文件的 Post请求   异步的
     * @param url
     * @param userkey
     * @param str
     * @param sign
     * @param picString  文件的地址
     * @return
     */
    public String xutilsFilePost( String url , String userkey , String str , String sign , String picString ){
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("userkey", userkey );
        params.addQueryStringParameter("str", str );
        params.addQueryStringParameter("sign", sign );
 
        // 只包含字符串参数时默认使用BodyParamsEntity,
        // 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。
        //params.addBodyParameter("name", "value");
 
        // 加入文件参数后默认使用MultipartEntity("multipart/form-data"),
        // 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。
        // 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:
        // MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
        // 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));
        params.addBodyParameter("picture"new File( picString )) ;
 
        com.lidroid.xutils.HttpUtils http = new com.lidroid.xutils.HttpUtils();
        http.send(HttpRequest.HttpMethod.POST ,
                url ,
                params,
                new RequestCallBack<String>() {
 
            @Override
            public void onStart() {
            }
 
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
            }
 
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                result = responseInfo.result.toString() ;
            }
 
            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
 
        return result ;
    }
 
    //-------------------以上的代码 是 异步请求的, 以下的代码是同步请求的-------------------------//<br>
    /**
     * Get同步请求 必须在异步块儿中执行
     * @param url
     * @param userkey
     * @param str
     * @param sign
     * @return
     */
    private String xutilsGetSync(String url , String userkey , String str , String sign ) {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("userkey", userkey );
        params.addQueryStringParameter("str", str );
        params.addQueryStringParameter("sign", sign );
        HttpUtils http = new HttpUtils() ;
        http.configCurrentHttpCacheExpiry(1000 10); //设置超时时间 
        try {
            ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.GET,
                    url ,
                    params ) ;
            //int statusCode = responseStream.getStatusCode();
            //Header[] headers = responseStream.getBaseResponse().getAllHeaders();
            return responseStream.readString();
        catch (Exception e) {
            LogUtils.e(e.getMessage(), e);
        }
        return null;
    }
     
    /**
     * Post同步请求 必须在异步块儿中执行
     * @param url
     * @param userkey
     * @param str
     * @param sign
     * @return
     */
    private String xutilsPostSync(String url , String userkey , String str , String sign ) {
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("userkey", userkey );
        params.addQueryStringParameter("str", str );
        params.addQueryStringParameter("sign", sign );
        HttpUtils http = new HttpUtils() ;
        http.configCurrentHttpCacheExpiry(1000 10); //设置超时时间 
        try {
            ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.POST ,
                    url ,
                    params ) ;
            //int statusCode = responseStream.getStatusCode();
            //Header[] headers = responseStream.getBaseResponse().getAllHeaders();
            return responseStream.readString();
        catch (Exception e) {
            LogUtils.e(e.getMessage(), e);
        }
        return null;
    }
}