java - Upload file to slack with Retrofit2 -
i'm trying upload file (heap dump) slack channel using retrofi2 latest release version.
@override public void oncreate() { super.oncreate(); slackapi = new retrofit.builder() .baseurl("https://slack.com/") .build() // .create(slackapi.class); } @multipart @gtconverterannotation(value = gtconverterannotation.gson) @post("api/files.upload") call<responsebody> uploadfile( @part("token") string token, @part("file") requestbody file, @part("filetype") string filetype, @part("filename") string filename, @part("title") string title, @part("initial_comment") string initialcomment, @part("channels") string channels); requestbody file = requestbody .create(mediatype.parse("multipart/form-data"), heapdump.heapdumpfile); final call<responsebody> call = slackapi.uploadfile(slackapi.token, file, null, heapdump.heapdumpfile.getname(), title, initialcomment, slackapi.memory_leak_channel);
the following code fail exception before execution @ "slack.uploafile" following exception:
> e/androidruntime: fatal exception: intentservice[com.squareup.leakcanary.abstractanalysisresultservice] process: com.gettaxi.dbx.android, pid: 11127 java.lang.illegalargumentexception: not locate requestbody converter class java.lang.string. tried: * retrofit2.builtinconverters @ retrofit2.retrofit.nextrequestbodyconverter(retrofit.java:298) @ retrofit2.retrofit.requestbodyconverter(retrofit.java:258) @ retrofit2.servicemethod$builder.parseparameterannotation(servicemethod.java:577) @ retrofit2.servicemethod$builder.parseparameter(servicemethod.java:328) @ retrofit2.servicemethod$builder.build(servicemethod.java:201) @ retrofit2.retrofit.loadservicemethod(retrofit.java:166) @ retrofit2.retrofit$1.invoke(retrofit.java:145) @ java.lang.reflect.proxy.invoke(proxy.java:397) @ $proxy13.uploadfile(unknown source) @ com.gettaxi.dbx.android.services.leakslackuploadservice.afterdefaulthandling(leakslackuploadservice.java:50) @ com.squareup.leakcanary.displayleakservice.onheapanalyzed(displayleakservice.java:86) @ com.squareup.leakcanary.abstractanalysisresultservice.onhandleintent(abstractanalysisresultservice.java:49) @ android.app.intentservice$servicehandler.handlemessage(intentservice.java:65) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.os.handlerthread.run(handlerthread.java:61)
what missing? why it's looking requestbody converter string?
update created full solution similar matrix suggest: https://gist.github.com/parahall/cbba57d9d10f6dcd850f
first want note there other ways of achieving went ahead making sure solution works no major drawbacks.
please check out repo here: update: url , repo name has changed https://github.com/matrixy/slackrofit
pasting relevant code well:
@multipart @post("api/files.upload") call<uploadfileresponse> uploadfile( @query("token") string token, @partmap map<string, requestbody> params, @query("filetype") string filetype, @query("filename") string filename, @query("title") string title, @query("initial_comment") string initialcomment, @query("channels") string channels); slackapi = new retrofit.builder().baseurl("https://slack.com/").client(new okhttpclient()) .addconverterfactory(gsonconverterfactory.create()) .build().create(slackapi.class); string str = "google places api android samples\n" + "===================================\n" + "\n" + "samples use [google places api android](https://developers.google.com/places/android/).\n" + "\n" + "this repo contains following samples:"; file = requestbody.create(mediatype.parse("multipart/form-data"), str.getbytes()); map<string, requestbody> map = new hashmap<>(); map.put("file\"; filename=\"heapdump.md\"", file); call = slackapi.uploadfile(slackapi.token, map, "text", "heapdump.md", "test dump", "check out", slackapi.memory_leak_channel);
later on activate call:
call.clone().enqueue(new callback<slackapi.uploadfileresponse>() { @override public void onresponse(call<slackapi.uploadfileresponse> call, response<slackapi.uploadfileresponse> response) { if (response != null) { log.e("gag", response.body().tostring()); } } @override public void onfailure(call<slackapi.uploadfileresponse> call, throwable t) { t.printstacktrace(); } });
i'm using clone test multiple uploads while allows me not rebuild new call every time want use it.
uploadfileresponse simple:
public static class uploadfileresponse { boolean ok; string error; @override public string tostring() { return "uploadfileresponse{" + "ok=" + ok + ", error='" + error + '\'' + '}'; } }
Comments
Post a Comment