java - Retrofit2.0 - how to POST image along with some parameters -
i've been trying send image file parameters user_id
, using retrofit, okhttp, gson, , rxandroid.
the problem following error:
500 internal server error
i can't upload file server. should change?
here's code.
homeactivity.java
gson gson = new gsonbuilder() .setfieldnamingpolicy(fieldnamingpolicy.lower_case_with_underscores) .registertypeadapter(date.class, new datetypeadapter()) .excludefieldswithoutexposeannotation() .create(); retrofit retrofit = new retrofit.builder() .baseurl(base_url) .addconverterfactory(gsonconverterfactory.create(gson)) .addcalladapterfactory(rxjavacalladapterfactory.create()) .build(); userapi uapi = retrofit.create(userapi.class); file file = new file("sdcard/dcim/camera/img_20131129_123723.jpg"); requestbody filebody = requestbody.create(mediatype.parse("image/jpeg"), file); map<string, object> map = new hashmap<>(); map.put("user_id", /*some string*/); map.put("tweet_id", /*some numbers*/); map.put("tweet_type", /*some numbers*/); this.subscription = uapi.postimage(map, filebody) .subscribeon(schedulers.io()) .observeon(androidschedulers.mainthread()) .subscribe(new observer<tweet>() { @override public void onnext(tweet tweet) {} @override public void oncompleted() {} @override public void onerror(throwable e) { e.printstacktrace(); } });
userapi.java
@headers("content-type: multipart/form-data") @multipart @post("static/write.json") public observable<tweet> postimage( @part("params") map map, @part("file\";filename=\"img_20131129_123723.jpg") requestbody file );
i tried append headers request myself, didn't work , same error before.
multipartbody mb = new multipartbody.builder("boundary") .addpart(headers.of("content-disposition","file; filename=\"img_20131129_123723.jpg\"; name=\"img_20131129_123723.jpg\""), uploadfile) .addpart(paramsbody) .build(); request request = new request.builder() .url("") .post(mb) .build();
build.gradle
dependencies { compile filetree(dir: 'libs', include: ['*.jar']) testcompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.code.gson:gson:2.4' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3' compile 'com.squareup.okhttp3:okhttp:3.0.0' compile 'io.reactivex:rxandroid:0.24.0' compile 'io.reactivex:rxjava:1.1.0' }
could please me?
Comments
Post a Comment