Android Photo Camera Upload with Retrofit - ASP.NET MVC Endpoint -
want able on fly post image back-end android app. doing wrong here? keep receiving internal server error, know endpoint , working (able call other clients)
i have asp.net mvc endpoint setup this:
[httppost] public jsonresult postimage(int id, string accesstoken, httppostedfilebase img)
in android code, using retrofit setup this:
@multipart @post("/endpoint/postimage") void postimage( @part("id") int id, @part("accesstoken") typedstring accesstoken, @part("img") typedbytearray img, callback<boolean> callback);
i have image handling code this:
bytearrayoutputstream bos = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.png, 0, bos); byte[] bitmapdata = bos.tobytearray(); // convert typed typedbytearray typedbytearray = new typedbytearray("application/octet-stream", bytes); typedstring str = new typedstring("mystring"); restadapter adapter = new restadapter.builder() .setendpoint("https://myendpoint.net").build(); apiservice apiservice = adapter.create(apiservice.class); apiservice.postimage(1492, str, typedbytearray, new callback<boolean>() { @override public void success(boolean success, response response) { boolean test = success; } @override public void failure(retrofiterror error) { //log.e("tappedin", error.getmessage()); } });
fyi i've resolved this!
here final code:
protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 1 && resultcode == result_ok) { bundle extras = data.getextras(); bitmap bitmap = (bitmap) extras.get("data"); bytearrayoutputstream out = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.jpeg, 65, out); byte[] bytes = out.tobytearray(); restadapter adapter = new restadapter.builder() .setendpoint("https://myendpoint.com").build(); apiservice apiservice = adapter.create(apiservice.class); typedbytearray typedbytearray = new typedbytearray("image/*", bytes) { @override public string filename() { return "img.jpg"; } }; typedstring myvar = new typedstring("abc"); apiservice.postvenuemenu(venueid, myvar, typedbytearray, new callback<successdto>() { @override public void success(successdto success, response response) { } @override public void failure(retrofiterror error) { } }); } }
Comments
Post a Comment