-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpLoadImage.java
60 lines (45 loc) · 1.93 KB
/
UpLoadImage.java
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
package com.optimais.pollingtest;
import android.util.Log;
import java.io.File;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import rx.Observable;
/**
* Created by tzhao on 2016/7/18.
*/
public class UpLoadImage {
public interface RetrofitImageUploadService {
@Multipart
@POST("EntranceGuardes/app/appOpen_pushdDataToApp.action")
Observable<UploadJsonResult> upload(@Part("userId") RequestBody description,
@Part MultipartBody.Part file);
}
private String imagePath;
private String userId;
public UpLoadImage(String userId, String imagePath) {
this.imagePath = imagePath;
this.userId = userId;
}
public Observable<UploadJsonResult> upload() {
File file = new File(imagePath);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://172.18.81.155:8080/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RetrofitImageUploadService service = retrofit.create(RetrofitImageUploadService.class);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
RequestBody description = RequestBody.create(MediaType.parse("text/plain"), userId);
return service.upload(description, body)
.doOnNext(uploadJsonResult -> Log.d("upload image", uploadJsonResult.getMessage()))
.doOnError(throwable -> Log.d("upload image", "upload failed"));
}
}