├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── ws │ │ └── retrofitview │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── ws │ │ │ └── retrofitview │ │ │ ├── DownFileActivity.java │ │ │ ├── GetActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── PostActivity.java │ │ │ ├── RxJavaActivity.java │ │ │ ├── SimpleGetActivity.java │ │ │ ├── UploadMultiFileActivity.java │ │ │ ├── UploadSimpleFileActivity.java │ │ │ ├── body │ │ │ └── CountingRequestBody.java │ │ │ ├── model │ │ │ ├── BaseResponse.java │ │ │ ├── Category.java │ │ │ ├── Manager.java │ │ │ └── Result.java │ │ │ ├── rest │ │ │ └── RestClient.java │ │ │ └── serviece │ │ │ └── RestService.java │ └── res │ │ ├── layout │ │ ├── activity_down_file.xml │ │ ├── activity_get.xml │ │ ├── activity_main.xml │ │ ├── activity_multi_file.xml │ │ ├── activity_simple_file.xml │ │ ├── activity_simple_get.xml │ │ └── rv_item.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── github │ └── ws │ └── retrofitview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | RetrofitView-master -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.7 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Retrofit2RxJava 2 | 3 | `Retrofit2`是`square`公司出品的一个网络请求库,网上有很多相关的介绍。我很久以前都想去研究了,但一直都有各种事情耽搁,现在就让我们一起去捋一捋,这篇主要讲解`Retrofit2`与`RxJava`的基本用法。 4 | 5 | - `get`请求 6 | 7 | - `post`请求 8 | 9 | - 文件上传 10 | 11 | - 文件下载 12 | 13 | - 开启日志拦截 14 | 15 | - 与RxJava结合使用 16 | 17 | ##什么是Retrofit2 18 | 19 | 官网是这么介绍的: 20 | 21 | ``` 22 | Retrofit adapts a Java interface to HTTP calls by using annotations on the declared methods to 23 | define how requests are made。 24 | ``` 25 | 26 | 我翻译的可能不准确,他的大概意思是说:Retrofit 是一个 java 接口类,以注解的方式用于 HTTP 网络请求。那下面我们一起来看看是怎么使用的? 27 | 28 | ##使用前的配置 29 | 30 | build.gradle 的 dependencies 添加: 31 | 32 | ``` 33 | compile 'com.google.code.gson:gson:2.3.1' 34 | compile 'com.squareup.retrofit2:retrofit:2.0.0' 35 | compile 'com.squareup.retrofit2:converter-gson:2.0.0' 36 | compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' 37 | ``` 38 | ---------- 39 | 40 | ##获取Retrofit实例 41 | 42 | ``` 43 | Retrofit retrofit = new Retrofit.Builder() 44 | .baseUrl("http://plus31.366ec.net/") 45 | .addConverterFactory(GsonConverterFactory.create()) 46 | .build(); 47 | ``` 48 | 49 | 需要注意的是`baseUrl`添加的是地址的主域名。 50 | 51 | ##申明RestService接口类 52 | 53 | ``` 54 | public interface RestService { 55 | @GET("/Route.axd?method=vast.Store.manager.list") 56 | Call getManagerData(@Query("StoreId") int id); 57 | } 58 | ``` 59 | 60 | `@GET` 包含的是请求地址,是主域名之后的地址。举个例子,请求的 61 | 62 | 全地址:`http://plus31.366ec.net/Route.axd?method=vast.Store.manager.list`, 63 | 64 | 主域名为:`http://plus31.366ec.net/` 65 | 66 | @GET包含的地址为:`/Route.axd?method=vast.Store.manager.list` 67 | 68 | 这样就完成了一个简单的`@GET`封装。 69 | 70 | ##创建RestClient类 71 | 72 | ``` 73 | public class RestClient { 74 | 75 | private Retrofit mRetrofit; 76 | private static final String BASE_URL = "http://plus31.366ec.net/"; 77 | private RestService mService; 78 | 79 | //构造方法 80 | public RestClient() { 81 | mRetrofit = new Retrofit.Builder() 82 | .baseUrl(BASE_URL) 83 | .addConverterFactory(GsonConverterFactory.create()) 84 | .build(); 85 | mService = mRetrofit.create(RestService.class); 86 | } 87 | 88 | public RestService getRectService() { 89 | if (mService != null) { 90 | return mService; 91 | } 92 | return null; 93 | } 94 | } 95 | 96 | ``` 97 | 98 | 这样就生成了一个简单的代理类,然后就可以进行相应请求了。 99 | 100 | ##Get请求 101 | 102 | ``` 103 | public class SimpleGetActivity extends AppCompatActivity { 104 | 105 | private Button btnGet; 106 | private TextView tvResult; 107 | 108 | private RestClient mRestClient; 109 | 110 | @Override 111 | protected void onCreate(@Nullable Bundle savedInstanceState) { 112 | super.onCreate(savedInstanceState); 113 | setContentView(R.layout.activity_simple_get); 114 | 115 | btnGet = (Button) findViewById(R.id.btn_get); 116 | tvResult = (TextView) findViewById(R.id.tv_result); 117 | 118 | 119 | btnGet.setOnClickListener(new View.OnClickListener() { 120 | @Override 121 | public void onClick(View view) { 122 | //获取实例 123 | mRestClient = new RestClient(); 124 | 125 | Call responseBodyCall = mRestClient.getRectService().getManagerData(49); 126 | //调用回调接口 127 | responseBodyCall.enqueue(new Callback() { 128 | @Override 129 | public void onResponse(Call call, Response response) { 130 | try { 131 | tvResult.setText(response.body().string()); 132 | } catch (IOException e) { 133 | e.printStackTrace(); 134 | } 135 | } 136 | 137 | @Override 138 | public void onFailure(Call call, Throwable t) { 139 | 140 | } 141 | }); 142 | } 143 | }); 144 | } 145 | } 146 | 147 | ``` 148 | 149 | 一起来看看效果: 150 | 151 | ![retro](http://img.blog.csdn.net/20160729102546858) 152 | 153 | ---------- 154 | 155 | 分析返回的 `json`数据,包含集合,那么我们可以进一步对接口返回值进行数据的封装。 156 | 157 | ###BaseResponse类 158 | 159 | ``` 160 | public class BaseResponse { 161 | 162 | @SerializedName("data") 163 | public List managerList; 164 | 165 | @SerializedName("code") 166 | public int code; 167 | 168 | @SerializedName("message") 169 | public String message; 170 | } 171 | ``` 172 | 173 | 注意:`BaseResponse`类的字段,根据自己返回`json`数据新增或者删除。 174 | 175 | 根据返回的`json`集合,那么我们肯定有个实体类了。 176 | 177 | ###Manager类 178 | 179 | ``` 180 | public class Manager { 181 | 182 | public int Id; 183 | 184 | public String UserName; 185 | 186 | } 187 | ``` 188 | 189 | `Manager` 类你可以替换成你自己的实体类。 190 | 191 | ###Get的进一步封装 192 | 193 | ``` 194 | @GET("/Route.axd?method=vast.Store.manager.list") 195 | Call> getManagerDatas(@Query("StoreId") int id); 196 | ``` 197 | 198 | 注意:我们这里对方法的返回值进行了一个修改`Call>` 199 | 200 | 来看看封装后的`Activity`类: 201 | 202 | ``` 203 | public class GetActivity extends AppCompatActivity { 204 | 205 | private Button btnGet; 206 | 207 | private RestClient mRestClient; 208 | 209 | private RecyclerView mRecyclerView; 210 | 211 | private BaseRecyclerAdapter mAdapter; 212 | 213 | @Override 214 | protected void onCreate(@Nullable Bundle savedInstanceState) { 215 | super.onCreate(savedInstanceState); 216 | setContentView(R.layout.activity_get); 217 | 218 | btnGet = (Button) findViewById(R.id.btn_get); 219 | mRecyclerView = (RecyclerView) findViewById(R.id.rv); 220 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 221 | 222 | btnGet.setOnClickListener(new View.OnClickListener() { 223 | @Override 224 | public void onClick(View view) { 225 | //获取实例 226 | mRestClient = new RestClient(); 227 | 228 | Call> baseResponseCall = mRestClient.getRectService().getManagerDatas(49); 229 | 230 | baseResponseCall.enqueue(new Callback>() { 231 | @Override 232 | public void onResponse(Call> call, Response> response) { 233 | //获取返回的集合数据 234 | //response.body().managerList 235 | mAdapter = new BaseRecyclerAdapter(GetActivity.this, response.body().managerList, R.layout.rv_item) { 236 | @Override 237 | protected void convert(BaseViewHolder helper, Manager item) { 238 | helper.setText(R.id.tv_item_text, item.UserName); 239 | } 240 | }; 241 | mRecyclerView.setAdapter(mAdapter); 242 | } 243 | 244 | @Override 245 | public void onFailure(Call> call, Throwable t) { 246 | 247 | } 248 | }); 249 | 250 | } 251 | }); 252 | } 253 | } 254 | ``` 255 | 256 | 来看看效果: 257 | 258 | ![retro](http://img.blog.csdn.net/20160729112322721) 259 | 260 | ---------- 261 | 262 | ###Get常用技巧 263 | 264 | `HashMap组装参数`: 265 | 266 | ``` 267 | @GET("/Route.axd?method=vast.Store.manager.list") 268 | Call> getManagerDatas(@QueryMap HashMap hm); 269 | ``` 270 | 271 | `Get` 请求就讲到这里了,下面一起来看看 `Post`请求。 272 | 273 | ##Post请求 274 | 275 | ``` 276 | @FormUrlEncoded 277 | @POST("/Route.axd?method=vast.Store.manager.list") 278 | Call> postManagerDatas(@Field("StoreId") int id); 279 | ``` 280 | 281 | `@Field("StoreId") int id`可以替换`@Body`,`@Body`你可以传入`HashMap`、实体 `beans` 等对象。 282 | 283 | 注意:以`@Body`上传参数,会默认加上`Content-Type: application/json;` `charset=UTF-8`的请求头,即以`JSON`格式请求,再以`JSON`格式响应。 284 | 285 | ##单个文件上传 286 | 287 | ``` 288 | @Multipart 289 | @POST("/UploadProduct.axd") 290 | Call uploadSimpleFile(@Part MultipartBody.Part file); 291 | ``` 292 | 293 | 文件上传稍微复杂点,具体请看以下代码: 294 | 295 | ``` 296 | File file = new File("/sdcard/", "a.xlxs"); 297 | //file 298 | RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 299 | //监听上传进度 300 | CountingRequestBody countingRequestBody = new CountingRequestBody(requestFile, new CountingRequestBody.Liste 301 | @Override 302 | public void onRequestProgress(long bytesWritten, long contentLength) { 303 | tvFile.setText("上传进度:" + contentLength + ":" + bytesWritten); 304 | } 305 | }); 306 | 307 | MultipartBody.Part body = 308 | MultipartBody.Part.createFormData("file", file.getName(),countingRequestBody); 309 | 310 | mRestClient = new RestClient("http://192.168.4.111:686/"); 311 | 312 | Call responseBodyCall = mRestClient.getRectService().uploadSimpleFile(body); 313 | 314 | responseBodyCall.enqueue(new Callback() { 315 | @Override 316 | public void onResponse(Call call, Response response) { 317 | tvFile.setText("上传成功"); 318 | } 319 | @Override 320 | public void onFailure(Call call, Throwable t) { 321 | tvFile.setText(t.toString()); 322 | } 323 | }); 324 | ``` 325 | 326 | 看看效果图: 327 | 328 | ![retro](http://img.blog.csdn.net/20160729142338985) 329 | 330 | 331 | ##多文件上传 332 | 333 | ``` 334 | @Multipart 335 | @POST("/HpWens/ProgressDemos/") 336 | Call uploads(@PartMap Map params); 337 | ``` 338 | 339 | ``` 340 | private void initData() { 341 | //保证文件按顺序上传 使用LinkedHashMap 342 | params = new LinkedHashMap<>(); 343 | 344 | File file1 = new File("/sdcard/", "a.xlxs"); 345 | final RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1); 346 | //监听上传进度 347 | CountingRequestBody countingRequestBody1 = new CountingRequestBody(requestBody1, new CountingRequestBody.Listener() { 348 | @Override 349 | public void onRequestProgress(long bytesWritten, long contentLength) { 350 | tvFile1.setText("上传进度:" + contentLength + ":" + bytesWritten); 351 | } 352 | }); 353 | 354 | params.put("file\";filename=\"" + file1.getName(), countingRequestBody1); 355 | 356 | 357 | File file2 = new File("/sdcard/", "a.xlxs"); 358 | RequestBody requestBody2 = RequestBody.create(MediaType.parse("multipart/form-data"), file2); 359 | //监听上传进度 360 | CountingRequestBody countingRequestBody2 = new CountingRequestBody(requestBody2, new CountingRequestBody.Listener() { 361 | @Override 362 | public void onRequestProgress(long bytesWritten, long contentLength) { 363 | tvFile2.setText("上传进度:" + contentLength + ":" + bytesWritten); 364 | } 365 | }); 366 | 367 | params.put("file\";filename=\"" + file2.getName(), countingRequestBody2); 368 | 369 | 370 | mRestClient = new RestClient("http://192.168.4.111:686/"); 371 | 372 | btnUpload.setOnClickListener(new View.OnClickListener() { 373 | @Override 374 | public void onClick(View view) { 375 | Call responseBodyCall = mRestClient.getRectService().uploadMultiFiles(params); 376 | responseBodyCall.enqueue(new Callback() { 377 | @Override 378 | public void onResponse(Call call, Response response) { 379 | tvFile1.setText("上传成功"); 380 | tvFile2.setText("上传成功"); 381 | } 382 | 383 | @Override 384 | public void onFailure(Call call, Throwable t) { 385 | 386 | } 387 | }); 388 | } 389 | }); 390 | 391 | } 392 | ``` 393 | 394 | 在文章的后面我会附上**源码**,这里我就不在贴图了,具体请看**demo** 395 | 396 | ##文件下载 397 | 398 | ``` 399 | @Streaming 400 | @GET("/image/h%3D360/sign=86aee1fbf1deb48fe469a7d8c01e3aef/{filename}") 401 | Call downFile(@Path("filename") String fileName); 402 | 403 | ``` 404 | 405 | 处理方式基本和上面几种差不多: 406 | 407 | ``` 408 | public class DownFileActivity extends AppCompatActivity { 409 | 410 | private ImageView iv; 411 | private Button btnDown; 412 | private RestClient mRestClient; 413 | 414 | private String fileName; 415 | 416 | @Override 417 | protected void onCreate(@Nullable Bundle savedInstanceState) { 418 | super.onCreate(savedInstanceState); 419 | setContentView(R.layout.activity_down_file); 420 | 421 | iv = (ImageView) findViewById(R.id.iv); 422 | btnDown = (Button) findViewById(R.id.btn_down); 423 | 424 | mRestClient = new RestClient("http://d.hiphotos.baidu.com/"); 425 | 426 | fileName = "b812c8fcc3cec3fd8757dcefd488d43f8794273a.jpg"; 427 | 428 | btnDown.setOnClickListener(new View.OnClickListener() { 429 | @Override 430 | public void onClick(View view) { 431 | Call userCall = mRestClient.getRectService().downFile(fileName); 432 | userCall.enqueue(new Callback() { 433 | @Override 434 | public void onResponse(Call call, Response response) { 435 | 436 | iv.setImageBitmap(BitmapFactory.decodeStream(response.body().byteStream())); 437 | //saveFile(response.body().byteStream()); 438 | } 439 | 440 | @Override 441 | public void onFailure(Call call, Throwable t) { 442 | 443 | } 444 | }); 445 | } 446 | }); 447 | } 448 | 449 | public void saveFile(InputStream is){ 450 | try { 451 | String fn = Environment.getExternalStorageDirectory() + "/" + fileName; 452 | FileOutputStream fos = new FileOutputStream(fn); 453 | byte[] buf = new byte[1024]; 454 | int len; 455 | while ((len = is.read(buf)) != -1) { 456 | fos.write(buf, 0, len); 457 | } 458 | is.close(); 459 | fos.close(); 460 | } catch (Exception ex) { 461 | 462 | } 463 | } 464 | } 465 | ``` 466 | 467 | 效果一览: 468 | 469 | ![retro](http://img.blog.csdn.net/20160729154449165) 470 | 471 | ---------- 472 | 473 | ##开启OKHttp的日志拦截 474 | 475 | 开启日志后,会记录request和response的相关信息,非常实用,也非常强大,不知道是否是编码格式,我下载图片打印的全是乱码。 476 | 477 | ``` 478 | public void initRestClint(String baseUrl) { 479 | logging.setLevel(HttpLoggingInterceptor.Level.BODY); 480 | OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 481 | httpClient.addInterceptor(logging); 482 | mRetrofit = new Retrofit.Builder() 483 | .baseUrl(baseUrl) 484 | .addConverterFactory(GsonConverterFactory.create()) 485 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 486 | .client(httpClient.build()) 487 | .build(); 488 | mService = mRetrofit.create(RestService.class); 489 | } 490 | ``` 491 | 492 | 类似这样的`logcat`日志: 493 | 494 | ![retro](http://img.blog.csdn.net/20160729155052152) 495 | 496 | 497 | ##Retrofit2与RxJava结合使用 498 | 499 | 添加库: 500 | 501 | ``` 502 | compile 'io.reactivex:rxandroid:1.1.0' 503 | compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0' 504 | ``` 505 | 506 | 添加`addCallAdapterFactory(RxJavaCallAdapterFactory.create())`到`Retrofit.Builder`中: 507 | 508 | ``` 509 | mRetrofit = new Retrofit.Builder() 510 | .baseUrl(baseUrl) 511 | .addConverterFactory(GsonConverterFactory.create()) 512 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 513 | .client(httpClient.build()) 514 | .build(); 515 | ``` 516 | 517 | 那么我们结合`RxJava`一起使用呢,下面我们一起来看一看: 518 | 519 | ``` 520 | @GET("/Route.axd?method=vast.Store.manager.list") 521 | Observable> getManagers(@Query("StoreId") int id); 522 | ``` 523 | 524 | 通过我们的观察是不是发现只有返回值发送了变化,`Observable`类型。 525 | 526 | ``` 527 | //获取实例 528 | mRestClient = new RestClient(); 529 | mRestClient.getRectService().getManagers(49) 530 | .subscribeOn(Schedulers.io()) 531 | .observeOn(AndroidSchedulers.mainThread()) 532 | .subscribe(new Subscriber>() { 533 | @Override 534 | public void onCompleted() { 535 | } 536 | @Override 537 | public void onError(Throwable e) { 538 | } 539 | @Override 540 | public void onNext(BaseResponse managerBaseResponse) { 541 | 542 | } 543 | }); 544 | ``` 545 | 546 | `RxJava`支持链式写法,可以处理一些很复杂的问题。 547 | 548 | [源码地址](https://github.com/HpWens/Retrofit2RxJava) 549 | 550 | 如果对你有所帮助,还请**stat**,欢迎加入**478720016** 来帮助更多的人 551 | 552 | 553 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.github.ws.retrofitview" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | 27 | compile 'io.reactivex:rxandroid:1.1.0' 28 | compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0' 29 | 30 | compile 'com.google.code.gson:gson:2.3.1' 31 | compile 'com.squareup.retrofit2:retrofit:2.0.0' 32 | compile 'com.squareup.retrofit2:converter-gson:2.0.0' 33 | compile 'com.squareup.okhttp3:logging-interceptor:3.2.0' 34 | 35 | compile 'io.reactivex:rxandroid:1.1.0' 36 | compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0' 37 | 38 | compile 'com.android.support:design:23.1.1' 39 | compile 'com.android.support:recyclerview-v7:23.4.0' 40 | compile 'com.android.support:cardview-v7:23.4.0' 41 | 42 | compile 'com.github.baserecycleradapter:library:1.0.6' 43 | } 44 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\androidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/github/ws/retrofitview/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/DownFileActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.graphics.BitmapFactory; 4 | import android.os.Bundle; 5 | import android.os.Environment; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.ImageView; 11 | 12 | import com.github.ws.retrofitview.rest.RestClient; 13 | 14 | import java.io.FileOutputStream; 15 | import java.io.InputStream; 16 | 17 | import okhttp3.ResponseBody; 18 | import retrofit2.Call; 19 | import retrofit2.Callback; 20 | import retrofit2.Response; 21 | 22 | /** 23 | * Created by Administrator on 7/29 0029. 24 | */ 25 | public class DownFileActivity extends AppCompatActivity { 26 | 27 | private ImageView iv; 28 | private Button btnDown; 29 | private RestClient mRestClient; 30 | 31 | private String fileName; 32 | 33 | @Override 34 | protected void onCreate(@Nullable Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_down_file); 37 | 38 | iv = (ImageView) findViewById(R.id.iv); 39 | btnDown = (Button) findViewById(R.id.btn_down); 40 | 41 | mRestClient = new RestClient("http://d.hiphotos.baidu.com/"); 42 | 43 | fileName = "b812c8fcc3cec3fd8757dcefd488d43f8794273a.jpg"; 44 | 45 | btnDown.setOnClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View view) { 48 | Call userCall = mRestClient.getRectService().downFile(fileName); 49 | userCall.enqueue(new Callback() { 50 | @Override 51 | public void onResponse(Call call, Response response) { 52 | 53 | iv.setImageBitmap(BitmapFactory.decodeStream(response.body().byteStream())); 54 | //saveFile(response.body().byteStream()); 55 | } 56 | 57 | @Override 58 | public void onFailure(Call call, Throwable t) { 59 | 60 | } 61 | }); 62 | } 63 | }); 64 | } 65 | 66 | public void saveFile(InputStream is){ 67 | try { 68 | String fn = Environment.getExternalStorageDirectory() + "/" + fileName; 69 | FileOutputStream fos = new FileOutputStream(fn); 70 | byte[] buf = new byte[1024]; 71 | int len; 72 | while ((len = is.read(buf)) != -1) { 73 | fos.write(buf, 0, len); 74 | } 75 | is.close(); 76 | fos.close(); 77 | } catch (Exception ex) { 78 | 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/GetActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.View; 9 | import android.widget.Button; 10 | 11 | import com.github.library.BaseRecyclerAdapter; 12 | import com.github.library.BaseViewHolder; 13 | import com.github.ws.retrofitview.model.BaseResponse; 14 | import com.github.ws.retrofitview.model.Manager; 15 | import com.github.ws.retrofitview.rest.RestClient; 16 | 17 | import retrofit2.Call; 18 | import retrofit2.Callback; 19 | import retrofit2.Response; 20 | 21 | /** 22 | * Created by Administrator on 7/29 0029. 23 | */ 24 | public class GetActivity extends AppCompatActivity { 25 | 26 | private Button btnGet; 27 | 28 | private RestClient mRestClient; 29 | 30 | private RecyclerView mRecyclerView; 31 | 32 | private BaseRecyclerAdapter mAdapter; 33 | 34 | @Override 35 | protected void onCreate(@Nullable Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_get); 38 | 39 | btnGet = (Button) findViewById(R.id.btn_get); 40 | mRecyclerView = (RecyclerView) findViewById(R.id.rv); 41 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 42 | 43 | btnGet.setOnClickListener(new View.OnClickListener() { 44 | @Override 45 | public void onClick(View view) { 46 | //获取实例 47 | mRestClient = new RestClient(); 48 | 49 | Call> baseResponseCall = mRestClient.getRectService().getManagerDatas(49); 50 | 51 | baseResponseCall.enqueue(new Callback>() { 52 | @Override 53 | public void onResponse(Call> call, Response> response) { 54 | //获取返回的集合数据 55 | //response.body().managerList 56 | mAdapter = new BaseRecyclerAdapter(GetActivity.this, response.body().managerList, R.layout.rv_item) { 57 | @Override 58 | protected void convert(BaseViewHolder helper, Manager item) { 59 | helper.setText(R.id.tv_item_text, item.UserName); 60 | } 61 | }; 62 | mRecyclerView.setAdapter(mAdapter); 63 | } 64 | 65 | @Override 66 | public void onFailure(Call> call, Throwable t) { 67 | 68 | } 69 | }); 70 | 71 | } 72 | }); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.View; 9 | 10 | import com.github.library.BaseRecyclerAdapter; 11 | import com.github.library.BaseViewHolder; 12 | import com.github.library.animation.AnimationType; 13 | import com.github.library.listener.OnRecyclerItemClickListener; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | public class MainActivity extends AppCompatActivity { 19 | 20 | // private RestClient mClient; 21 | // 22 | // public static String KEY = "Rsg+8rx5M38xinuYepD+oQiUdUE="; 23 | // 24 | // public String mFileName; 25 | 26 | private RecyclerView mRecyclerView; 27 | 28 | private BaseRecyclerAdapter mAdapter; 29 | 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setContentView(R.layout.activity_main); 35 | 36 | mRecyclerView = (RecyclerView) findViewById(R.id.rv); 37 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 38 | mRecyclerView.setAdapter(mAdapter = new BaseRecyclerAdapter(this, getDatas(), R.layout.rv_item) { 39 | @Override 40 | protected void convert(BaseViewHolder helper, String item) { 41 | helper.setText(R.id.tv_item_text, item); 42 | } 43 | }); 44 | mAdapter.openLoadAnimation(AnimationType.SCALE); 45 | 46 | mAdapter.setOnRecyclerItemClickListener(new OnRecyclerItemClickListener() { 47 | @Override 48 | public void onItemClick(View view, int position) { 49 | Intent intent = null; 50 | switch (position) { 51 | case 0: 52 | intent = new Intent(MainActivity.this, SimpleGetActivity.class); 53 | break; 54 | case 1: 55 | intent = new Intent(MainActivity.this, GetActivity.class); 56 | break; 57 | case 2: 58 | intent = new Intent(MainActivity.this, PostActivity.class); 59 | break; 60 | case 3: 61 | intent = new Intent(MainActivity.this, UploadSimpleFileActivity.class); 62 | break; 63 | case 4: 64 | intent = new Intent(MainActivity.this, UploadMultiFileActivity.class); 65 | break; 66 | case 5: 67 | intent = new Intent(MainActivity.this, DownFileActivity.class); 68 | break; 69 | case 6: 70 | intent = new Intent(MainActivity.this, RxJavaActivity.class); 71 | break; 72 | 73 | } 74 | startActivity(intent); 75 | } 76 | }); 77 | 78 | 79 | } 80 | 81 | public List getDatas() { 82 | List datas = new ArrayList<>(); 83 | datas.add("Simple Get Activity"); 84 | datas.add("Get Activity"); 85 | datas.add("Post Activity"); 86 | datas.add("Upload Simple File Activity"); 87 | datas.add("Upload multi File Activity"); 88 | datas.add("Down File Activity"); 89 | datas.add("RxJava Activity"); 90 | return datas; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/PostActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.View; 9 | import android.widget.Button; 10 | 11 | import com.github.library.BaseRecyclerAdapter; 12 | import com.github.library.BaseViewHolder; 13 | import com.github.ws.retrofitview.model.BaseResponse; 14 | import com.github.ws.retrofitview.model.Manager; 15 | import com.github.ws.retrofitview.rest.RestClient; 16 | 17 | import retrofit2.Call; 18 | import retrofit2.Callback; 19 | import retrofit2.Response; 20 | 21 | /** 22 | * Created by Administrator on 7/29 0029. 23 | */ 24 | public class PostActivity extends AppCompatActivity { 25 | 26 | private Button btnGet; 27 | 28 | private RestClient mRestClient; 29 | 30 | private RecyclerView mRecyclerView; 31 | 32 | private BaseRecyclerAdapter mAdapter; 33 | 34 | @Override 35 | protected void onCreate(@Nullable Bundle savedInstanceState) { 36 | super.onCreate(savedInstanceState); 37 | setContentView(R.layout.activity_get); 38 | 39 | btnGet = (Button) findViewById(R.id.btn_get); 40 | mRecyclerView = (RecyclerView) findViewById(R.id.rv); 41 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 42 | btnGet.setText("Post请求"); 43 | btnGet.setOnClickListener(new View.OnClickListener() { 44 | @Override 45 | public void onClick(View view) { 46 | //获取实例 47 | mRestClient = new RestClient(); 48 | 49 | Call> baseResponseCall = mRestClient.getRectService().postManagerDatas(49); 50 | 51 | baseResponseCall.enqueue(new Callback>() { 52 | @Override 53 | public void onResponse(Call> call, Response> response) { 54 | //获取返回的集合数据 55 | //response.body().managerList 56 | mAdapter = new BaseRecyclerAdapter(PostActivity.this, response.body().managerList, R.layout.rv_item) { 57 | @Override 58 | protected void convert(BaseViewHolder helper, Manager item) { 59 | helper.setText(R.id.tv_item_text, item.UserName); 60 | } 61 | }; 62 | mRecyclerView.setAdapter(mAdapter); 63 | } 64 | 65 | @Override 66 | public void onFailure(Call> call, Throwable t) { 67 | 68 | } 69 | }); 70 | 71 | } 72 | }); 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/RxJavaActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.View; 9 | import android.widget.Button; 10 | 11 | import com.github.library.BaseRecyclerAdapter; 12 | import com.github.library.BaseViewHolder; 13 | import com.github.ws.retrofitview.model.BaseResponse; 14 | import com.github.ws.retrofitview.model.Manager; 15 | import com.github.ws.retrofitview.rest.RestClient; 16 | 17 | import rx.Subscriber; 18 | import rx.android.schedulers.AndroidSchedulers; 19 | import rx.schedulers.Schedulers; 20 | 21 | /** 22 | * Created by Administrator on 7/29 0029. 23 | */ 24 | public class RxJavaActivity extends AppCompatActivity { 25 | private Button btnGet; 26 | 27 | private RestClient mRestClient; 28 | 29 | private RecyclerView mRecyclerView; 30 | 31 | private BaseRecyclerAdapter mAdapter; 32 | 33 | @Override 34 | protected void onCreate(@Nullable Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_get); 37 | 38 | btnGet = (Button) findViewById(R.id.btn_get); 39 | mRecyclerView = (RecyclerView) findViewById(R.id.rv); 40 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 41 | 42 | btnGet.setOnClickListener(new View.OnClickListener() { 43 | @Override 44 | public void onClick(View view) { 45 | //获取实例 46 | mRestClient = new RestClient(); 47 | 48 | mRestClient.getRectService().getManagers(49) 49 | .subscribeOn(Schedulers.io()) 50 | .observeOn(AndroidSchedulers.mainThread()) 51 | .subscribe(new Subscriber>() { 52 | @Override 53 | public void onCompleted() { 54 | 55 | } 56 | 57 | @Override 58 | public void onError(Throwable e) { 59 | 60 | } 61 | 62 | @Override 63 | public void onNext(BaseResponse managerBaseResponse) { 64 | //获取返回的集合数据 65 | mAdapter = new BaseRecyclerAdapter(RxJavaActivity.this, managerBaseResponse.managerList, R.layout.rv_item) { 66 | @Override 67 | protected void convert(BaseViewHolder helper, Manager item) { 68 | helper.setText(R.id.tv_item_text, item.UserName); 69 | } 70 | }; 71 | mRecyclerView.setAdapter(mAdapter); 72 | } 73 | }); 74 | 75 | } 76 | }); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/SimpleGetActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.TextView; 9 | 10 | import com.github.ws.retrofitview.rest.RestClient; 11 | 12 | import java.io.IOException; 13 | 14 | import okhttp3.ResponseBody; 15 | import retrofit2.Call; 16 | import retrofit2.Callback; 17 | import retrofit2.Response; 18 | 19 | /** 20 | * Created by Administrator on 7/29 0029. 21 | */ 22 | public class SimpleGetActivity extends AppCompatActivity { 23 | 24 | private Button btnGet; 25 | private TextView tvResult; 26 | 27 | private RestClient mRestClient; 28 | 29 | @Override 30 | protected void onCreate(@Nullable Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_simple_get); 33 | 34 | btnGet = (Button) findViewById(R.id.btn_get); 35 | tvResult = (TextView) findViewById(R.id.tv_result); 36 | 37 | 38 | btnGet.setOnClickListener(new View.OnClickListener() { 39 | @Override 40 | public void onClick(View view) { 41 | //获取实例 42 | mRestClient = new RestClient(); 43 | 44 | Call responseBodyCall = mRestClient.getRectService().getManagerData(49); 45 | //调用回调接口 46 | responseBodyCall.enqueue(new Callback() { 47 | @Override 48 | public void onResponse(Call call, Response response) { 49 | try { 50 | tvResult.setText(response.body().string()); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | 56 | @Override 57 | public void onFailure(Call call, Throwable t) { 58 | 59 | } 60 | }); 61 | 62 | } 63 | }); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/UploadMultiFileActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.TextView; 9 | 10 | import com.github.ws.retrofitview.body.CountingRequestBody; 11 | import com.github.ws.retrofitview.rest.RestClient; 12 | 13 | import java.io.File; 14 | import java.util.LinkedHashMap; 15 | import java.util.Map; 16 | 17 | import okhttp3.MediaType; 18 | import okhttp3.RequestBody; 19 | import okhttp3.ResponseBody; 20 | import retrofit2.Call; 21 | import retrofit2.Callback; 22 | import retrofit2.Response; 23 | 24 | /** 25 | * Created by Administrator on 7/29 0029. 26 | */ 27 | public class UploadMultiFileActivity extends AppCompatActivity { 28 | 29 | //保证文件按顺序上传 使用LinkedHashMap 30 | Map params; 31 | 32 | private TextView tvFile1; 33 | private TextView tvFile2; 34 | private Button btnUpload; 35 | 36 | private RestClient mRestClient; 37 | 38 | @Override 39 | protected void onCreate(@Nullable Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_multi_file); 42 | 43 | tvFile1 = (TextView) findViewById(R.id.tv_file1); 44 | tvFile2 = (TextView) findViewById(R.id.tv_file2); 45 | btnUpload = (Button) findViewById(R.id.btn_upload); 46 | 47 | initData(); 48 | 49 | } 50 | 51 | private void initData() { 52 | 53 | params = new LinkedHashMap<>(); 54 | 55 | File file1 = new File("/sdcard/", "a.xlxs"); 56 | final RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1); 57 | //监听上传进度 58 | CountingRequestBody countingRequestBody1 = new CountingRequestBody(requestBody1, new CountingRequestBody.Listener() { 59 | @Override 60 | public void onRequestProgress(long bytesWritten, long contentLength) { 61 | tvFile1.setText("上传进度:" + contentLength + ":" + bytesWritten); 62 | } 63 | }); 64 | 65 | params.put("file\";filename=\"" + file1.getName(), countingRequestBody1); 66 | 67 | 68 | File file2 = new File("/sdcard/", "a.xlxs"); 69 | RequestBody requestBody2 = RequestBody.create(MediaType.parse("multipart/form-data"), file2); 70 | //监听上传进度 71 | CountingRequestBody countingRequestBody2 = new CountingRequestBody(requestBody2, new CountingRequestBody.Listener() { 72 | @Override 73 | public void onRequestProgress(long bytesWritten, long contentLength) { 74 | tvFile2.setText("上传进度:" + contentLength + ":" + bytesWritten); 75 | } 76 | }); 77 | 78 | params.put("file\";filename=\"" + file2.getName(), countingRequestBody2); 79 | 80 | 81 | mRestClient = new RestClient("http://192.168.4.111:686/"); 82 | 83 | btnUpload.setOnClickListener(new View.OnClickListener() { 84 | @Override 85 | public void onClick(View view) { 86 | Call responseBodyCall = mRestClient.getRectService().uploadMultiFiles(params); 87 | responseBodyCall.enqueue(new Callback() { 88 | @Override 89 | public void onResponse(Call call, Response response) { 90 | tvFile1.setText("上传成功"); 91 | tvFile2.setText("上传成功"); 92 | } 93 | 94 | @Override 95 | public void onFailure(Call call, Throwable t) { 96 | 97 | } 98 | }); 99 | } 100 | }); 101 | 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/UploadSimpleFileActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview; 2 | 3 | import android.os.AsyncTask; 4 | import android.os.Bundle; 5 | import android.os.Handler; 6 | import android.os.Message; 7 | import android.support.annotation.Nullable; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.view.View; 10 | import android.widget.Button; 11 | import android.widget.TextView; 12 | 13 | import com.github.ws.retrofitview.body.CountingRequestBody; 14 | import com.github.ws.retrofitview.rest.RestClient; 15 | 16 | import java.io.File; 17 | 18 | import okhttp3.MediaType; 19 | import okhttp3.MultipartBody; 20 | import okhttp3.RequestBody; 21 | import okhttp3.ResponseBody; 22 | import retrofit2.Call; 23 | import retrofit2.Callback; 24 | import retrofit2.Response; 25 | 26 | /** 27 | * Created by Administrator on 7/29 0029. 28 | */ 29 | public class UploadSimpleFileActivity extends AppCompatActivity { 30 | 31 | private TextView tvFile; 32 | private Button btnUpload; 33 | private RestClient mRestClient; 34 | 35 | private Handler mHandler = new Handler() { 36 | @Override 37 | public void handleMessage(Message msg) { 38 | super.handleMessage(msg); 39 | } 40 | }; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_simple_file); 46 | 47 | tvFile = (TextView) findViewById(R.id.tv_file); 48 | btnUpload = (Button) findViewById(R.id.btn_upload); 49 | 50 | btnUpload.setOnClickListener(new View.OnClickListener() { 51 | @Override 52 | public void onClick(View view) { 53 | new AsyncTask() { 54 | 55 | @Override 56 | protected Void doInBackground(Void... voids) { 57 | File file = new File("/sdcard/", "a.xlxs"); 58 | //file 59 | RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); 60 | 61 | //监听上传进度 62 | CountingRequestBody countingRequestBody = new CountingRequestBody(requestFile, new CountingRequestBody.Listener() { 63 | @Override 64 | public void onRequestProgress(long bytesWritten, long contentLength) { 65 | tvFile.setText("上传进度:" + contentLength + ":" + bytesWritten); 66 | } 67 | }); 68 | 69 | MultipartBody.Part body = 70 | MultipartBody.Part.createFormData("file", file.getName(),countingRequestBody); 71 | 72 | mRestClient = new RestClient("http://192.168.4.111:686/"); 73 | 74 | Call responseBodyCall = mRestClient.getRectService().uploadSimpleFile(body); 75 | 76 | responseBodyCall.enqueue(new Callback() { 77 | @Override 78 | public void onResponse(Call call, Response response) { 79 | tvFile.setText("上传成功"); 80 | } 81 | 82 | @Override 83 | public void onFailure(Call call, Throwable t) { 84 | tvFile.setText(t.toString()); 85 | } 86 | }); 87 | return null; 88 | } 89 | }.execute(); 90 | 91 | } 92 | }); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/body/CountingRequestBody.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.body; 2 | 3 | import java.io.IOException; 4 | 5 | import okhttp3.MediaType; 6 | import okhttp3.RequestBody; 7 | import okio.Buffer; 8 | import okio.BufferedSink; 9 | import okio.ForwardingSink; 10 | import okio.Okio; 11 | import okio.Sink; 12 | 13 | /** 14 | * Created by Administrator on 5/18 0018. 15 | */ 16 | public class CountingRequestBody extends RequestBody { 17 | 18 | protected RequestBody delegate; 19 | protected Listener listener; 20 | 21 | protected CountingSink countingSink; 22 | 23 | public CountingRequestBody(RequestBody delegate, Listener listener) { 24 | this.delegate = delegate; 25 | this.listener = listener; 26 | } 27 | 28 | @Override 29 | public MediaType contentType() { 30 | return delegate.contentType(); 31 | } 32 | 33 | @Override 34 | public long contentLength() { 35 | try { 36 | return delegate.contentLength(); 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } 40 | return -1; 41 | } 42 | 43 | @Override 44 | public void writeTo(BufferedSink sink) { 45 | countingSink = new CountingSink(sink); 46 | BufferedSink bufferedSink = null; 47 | try { 48 | bufferedSink = Okio.buffer(countingSink); 49 | delegate.writeTo(bufferedSink); 50 | } catch (IOException e) { 51 | e.printStackTrace(); 52 | try { 53 | bufferedSink.flush(); 54 | } catch (IOException e1) { 55 | e1.printStackTrace(); 56 | } 57 | } 58 | } 59 | 60 | protected final class CountingSink extends ForwardingSink { 61 | 62 | private long bytesWritten = 0; 63 | 64 | public CountingSink(Sink delegate) { 65 | super(delegate); 66 | } 67 | 68 | @Override 69 | public void write(Buffer source, long byteCount) { 70 | try { 71 | super.write(source, byteCount); 72 | if (listener != null) { 73 | bytesWritten += byteCount; 74 | listener.onRequestProgress(bytesWritten, contentLength()); 75 | } 76 | } catch (IOException e) { 77 | e.printStackTrace(); 78 | } 79 | } 80 | } 81 | 82 | public interface Listener { 83 | void onRequestProgress(long bytesWritten, long contentLength); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/model/BaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | */ 9 | public class BaseResponse { 10 | 11 | @SerializedName("data") 12 | public List managerList; 13 | 14 | @SerializedName("code") 15 | public int code; 16 | 17 | @SerializedName("message") 18 | public String message; 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/model/Category.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.model; 2 | 3 | /** 4 | * Created by Administrator on 5/18 0018. 5 | */ 6 | public class Category { 7 | 8 | 9 | /** 10 | * categoryid : 4 11 | * parentid : 0 12 | * name : 书籍 13 | * status : 1 14 | * sortorder : 0 15 | * remark : 16 | * productcount : 2 17 | */ 18 | 19 | public int categoryid; 20 | public int parentid; 21 | public String name; 22 | public int status; 23 | public int sortorder; 24 | public String remark; 25 | public String productcount; 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/model/Manager.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.model; 2 | 3 | /** 4 | * Created by Administrator on 7/29 0029. 5 | */ 6 | public class Manager { 7 | 8 | public int Id; 9 | 10 | public String UserName; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/model/Result.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.model; 2 | 3 | /** 4 | * Created by Administrator on 5/18 0018. 5 | */ 6 | public class Result { 7 | 8 | String ResultMessage; 9 | int ResultCode; 10 | T Data; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/rest/RestClient.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.rest; 2 | 3 | import com.github.ws.retrofitview.serviece.RestService; 4 | 5 | import okhttp3.OkHttpClient; 6 | import okhttp3.logging.HttpLoggingInterceptor; 7 | import retrofit2.Retrofit; 8 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; 9 | import retrofit2.converter.gson.GsonConverterFactory; 10 | 11 | /** 12 | * Created by Administrator on 5/17 0017. 13 | */ 14 | public class RestClient { 15 | 16 | private Retrofit mRetrofit; 17 | 18 | private static final String BASE_URL = "http://plus31.366ec.net/"; 19 | private RestService mService; 20 | 21 | HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 22 | 23 | public RestClient() { 24 | initRestClint(BASE_URL); 25 | } 26 | 27 | 28 | public RestClient(String baseUrl) { 29 | initRestClint(baseUrl); 30 | } 31 | 32 | public void initRestClint(String baseUrl) { 33 | logging.setLevel(HttpLoggingInterceptor.Level.BODY); 34 | 35 | OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 36 | 37 | httpClient.addInterceptor(logging); 38 | 39 | mRetrofit = new Retrofit.Builder() 40 | .baseUrl(baseUrl) 41 | .addConverterFactory(GsonConverterFactory.create()) 42 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 43 | .client(httpClient.build()) 44 | .build(); 45 | 46 | mService = mRetrofit.create(RestService.class); 47 | } 48 | 49 | 50 | public RestService getRectService() { 51 | if (mService != null) { 52 | return mService; 53 | } 54 | return null; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/ws/retrofitview/serviece/RestService.java: -------------------------------------------------------------------------------- 1 | package com.github.ws.retrofitview.serviece; 2 | 3 | import com.github.ws.retrofitview.model.BaseResponse; 4 | import com.github.ws.retrofitview.model.Manager; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import okhttp3.MultipartBody; 10 | import okhttp3.RequestBody; 11 | import okhttp3.ResponseBody; 12 | import retrofit2.Call; 13 | import retrofit2.http.Field; 14 | import retrofit2.http.FormUrlEncoded; 15 | import retrofit2.http.GET; 16 | import retrofit2.http.Multipart; 17 | import retrofit2.http.POST; 18 | import retrofit2.http.Part; 19 | import retrofit2.http.PartMap; 20 | import retrofit2.http.Path; 21 | import retrofit2.http.Query; 22 | import retrofit2.http.QueryMap; 23 | import retrofit2.http.Streaming; 24 | import rx.Observable; 25 | 26 | /** 27 | * Created by Administrator on 5/17 0017. 28 | */ 29 | 30 | public interface RestService { 31 | 32 | @GET("/Route.axd?method=vast.Store.manager.list") 33 | Call getManagerData(@Query("StoreId") int id); 34 | 35 | 36 | @GET("/Route.axd?method=vast.Store.manager.list") 37 | Call> getManagerDatas(@Query("StoreId") int id); 38 | 39 | @GET("/Route.axd?method=vast.Store.manager.list") 40 | Call> getManagerDatas(@QueryMap HashMap hm); 41 | 42 | 43 | @FormUrlEncoded 44 | @POST("/Route.axd?method=vast.Store.manager.list") 45 | Call> postManagerDatas(@Field("StoreId") int id); 46 | 47 | 48 | @Multipart 49 | @POST("/UploadProduct.axd") 50 | Call uploadSimpleFile(@Part MultipartBody.Part file); 51 | 52 | @Multipart 53 | @POST("/UploadProduct.axd") 54 | Call uploadMultiFiles(@PartMap Map params); 55 | 56 | 57 | @Streaming 58 | @GET("/image/h%3D360/sign=86aee1fbf1deb48fe469a7d8c01e3aef/{filename}") 59 | Call downFile(@Path("filename") String fileName); 60 | 61 | 62 | @GET("/Route.axd?method=vast.Store.manager.list") 63 | Observable> getManagers(@Query("StoreId") int id); 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_down_file.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 |