web services - how to upload a file using multipart form data in android -
how use multipart-form data(@formdataparamdata) uploading file in android. trying upload image file android end multipart form data.
here server script found @ server side: public image upload(@formdataparam("image") inputstream istream, @formdataparam("image") formdatabodypart body, @formdataparam("eventid") long eventid, @formdataparam("eventdescription") string eventdescription) throws imagewebserviceexception { // ...... } here client code tried uploading in android... client client = new client(); client.addfilter(new httpbasicauthfilter("user123", "******")); webresource webresource = client.resource(url); clientresponse response; formdatamultipart form = new formdatamultipart(); file file = new file("f:/android.png"); form.field("eventid", "1"); form.field("eventdescription", "password"); form.field("image", file.getname()); form.bodypart(new filedatabodypart("image", file, mediatype.valueof("image/png"))); response = webresource.type(mediatype.multipart_form_data).post(clientresponse.class, form); above code im getting 415 status code error unsupported media type. thanks.
try httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httppost httppost = new httppost(url); try { multipartentity entity = new multipartentity(httpmultipartmode.browser_compatible); (int index = 0; index < namevaluepairs.size(); index++) { entity.addpart(namevaluepairs.get(index).getname(), new filebody(new file(namevaluepairs.get(index) .getvalue()))); } httppost.setentity(entity); httpresponse response = httpclient.execute(httppost, localcontext); httpentity resentity = response.getentity(); if (resentity != null) { page = entityutils.tostring(resentity); system.out.println("page :" + page); } } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment