android - Upload base64 encoded image using HttPost and HttpClient -
android - Upload base64 encoded image using HttPost and HttpClient -
i seek upload image (base64 encoded) alongside other values php webserver, takes info , farther processing.
the folowing functions part of asynctask, executes request.
i build postdata hand concatenating different values.
the jsonurllook class helps me finding right url sending info to. verified it. correct.
when seek execute created httppost response null. thought why is?
private string getpostdata(string arraystring, string base64image, string boundary) { string remotemethod = "uploadpic"; string postdata = ""; // add together remote method name postdata += "--" + boundary + "\r\n"; postdata += "content-disposition: form-data; name=\"" + boundary + "[method]\"\r\n\r\n"; postdata += remotemethod; postdata += "--" + boundary + "\r\n"; postdata += "content-disposition: form-data; name=\"" + boundary + "[params]\"\r\n\r\n"; postdata += arraystring; postdata += "--" + boundary + "\r\n"; postdata += "content-disposition: form-data; name=\"" + boundary + "\"\r\n\r\n"; postdata += "filename=\"image.jpg\"\r\n\r\n"; postdata += base64image; postdata += "--" + boundary + "--"; homecoming postdata; } private string uploadpicture(string arraystring, string base64image) { string boundary = "tx_fpoemobile_pi2"; // post info string postdata = getpostdata(arraystring, base64image, boundary); // webservice uri jsonurllookup lookup = new jsonurllookup(context); url webserviceurl = lookup.buildurl(context.getresources().getstring(r.string.page_uid_webservice)); uri webserviceuri = null; seek { webserviceuri = new uri(webserviceurl.tostring()); } catch(exception e) { // todo } // build client , post httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(webserviceuri); // set header of post httppost.setheader("content-type", "multipart/form-data; boundary=" + boundary); httppost.setheader("content-length", string.valueof(postdata.length())); // set body seek { httppost.setentity(new stringentity(postdata)); } grab (exception e) { // todo } httpresponse response = null; seek { response = httpclient.execute(httppost); } grab (clientprotocolexception e) { // todo } grab (ioexception e) { // todo } homecoming "uploadpicturesuccess"; }
you have work
public void connectformultipart() throws exception { con = (httpurlconnection) ( new url(url)).openconnection(); con.setrequestmethod("post"); con.setdoinput(true); con.setdooutput(true); con.setrequestproperty("connection", "keep-alive"); con.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); con.connect(); os = con.getoutputstream(); } public void addformpart(string paramname, string value) throws exception { writeparamdata(paramname, value); } public void addfilepart(string paramname, string filename, byte[] data) throws exception { os.write( (delimiter + boundary + "\r\n").getbytes()); os.write( ("content-disposition: form-data; name=\"" + paramname + "\"; filename=\"" + filename + "\"\r\n" ).getbytes()); os.write( ("content-type: application/octet-stream\r\n" ).getbytes()); os.write( ("content-transfer-encoding: binary\r\n" ).getbytes()); os.write("\r\n".getbytes()); os.write(data); os.write("\r\n".getbytes()); } public void finishmultipart() throws exception { os.write( (delimiter + boundary + delimiter + "\r\n").getbytes()); }
for getting response httppost.setentity(entity); httpresponse response = httpclient.execute(httppost); bufferedreader reader = new bufferedreader(new inputstreamreader(response.getentity().getcontent(), "utf-8")); string sresponse; while ((sresponse = reader.readline()) != null) { s = s.append(sresponse); } if(response.getstatusline().getstatuscode() == httpstatus.sc_ok) { homecoming s.tostring(); }else { homecoming "{\"status\":\"false\",\"message\":\"some error occurred\"}"; } } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); }
in place of image can send anything.
in case need httpmime-4.2.1-1.jar
android image http-post multipart
Comments
Post a Comment