Cannot Send Text along with Image to Server [android] -
hi how can send text message along image
can upload image cannot send text @ once want send string user_id , project_id along this.
help me please.
private class uploadtask extends asynctask<bitmap, void, void> { protected void doinbackground(bitmap... bitmaps) { if (bitmaps[0] == null) return null; setprogress(0); bitmap bitmap = bitmaps[0]; bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.png, 100, stream); // convert bitmap bytearrayoutputstream inputstream in = new bytearrayinputstream(stream.tobytearray()); // convert bytearrayoutputstream bytearrayinputstream defaulthttpclient httpclient = new defaulthttpclient(); try { httppost httppost = new httppost( "http://61.19.244.132/upload.php"); // server stringbody user_id = new stringbody("user_id", contenttype.multipart_form_data); stringbody project_id = new stringbody("project_id", contenttype.multipart_form_data); multipartentitybuilder builder = multipartentitybuilder.create(); builder.setmode(httpmultipartmode.browser_compatible); builder.addpart("user_id", user_id); builder.addpart("project_id", project_id); httpentity entity = builder.build(); httppost.setentity(entity); multipartentity reqentity = new multipartentity(); reqentity.addpart("filedata", system.currenttimemillis() + ".jpg", in); httppost.setentity(reqentity); log.i(tag, "request " + httppost.getrequestline()); httpresponse response = null; try { response = httpclient.execute(httppost); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { if (response != null) log.i(tag, "response " + response.getstatusline().tostring()); } { } } { } if (in != null) { try { in.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } if (stream != null) { try { stream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return null; } @override protected void onprogressupdate(void... values) { // todo auto-generated method stub super.onprogressupdate(values); } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); toast.maketext(mainactivity.this, r.string.uploaded, toast.length_long).show(); } }
multipartentity.java
public class multipartentity implements httpentity { private string boundary = null; bytearrayoutputstream out = new bytearrayoutputstream(); boolean issetlast = false; boolean issetfirst = false; public multipartentity() { this.boundary = system.currenttimemillis() + ""; } public void writefirstboundaryifneeds(){ if(!issetfirst){ try { out.write(("--" + boundary + "\r\n").getbytes()); } catch (final ioexception e) { } } issetfirst = true; } public void writelastboundaryifneeds() { if(issetlast){ return ; } try { out.write(("\r\n--" + boundary + "--\r\n").getbytes()); } catch (final ioexception e) { } issetlast = true; } public void addpart(final string key, final string value) { writefirstboundaryifneeds(); try { out.write(("content-disposition: form-data; name=\"" +key+"\"\r\n").getbytes()); out.write("content-type: text/plain; charset=utf-8\r\n".getbytes()); out.write("content-transfer-encoding: 8bit\r\n\r\n".getbytes()); out.write(value.getbytes()); out.write(("\r\n--" + boundary + "\r\n").getbytes()); } catch (final ioexception e) { } } public void addpart(final string key, final string filename, final inputstream fin){ addpart(key, filename, fin, "application/octet-stream"); } public void addpart(final string key, final string filename, final inputstream fin, string type){ writefirstboundaryifneeds(); try { type = "content-type: "+type+"\r\n"; out.write(("content-disposition: form-data; name=\""+ key+"\"; filename=\"" + filename + "\"\r\n").getbytes()); out.write(type.getbytes()); out.write("content-transfer-encoding: binary\r\n\r\n".getbytes()); final byte[] tmp = new byte[4096]; int l = 0; while ((l = fin.read(tmp)) != -1) { out.write(tmp, 0, l); } out.flush(); } catch (final ioexception e) { } { try { fin.close(); } catch (final ioexception e) { } } } public void addpart(final string key, final file value) { try { addpart(key, value.getname(), new fileinputstream(value)); } catch (final filenotfoundexception e) { } } @override public long getcontentlength() { writelastboundaryifneeds(); return out.tobytearray().length; } @override public header getcontenttype() { return new basicheader("content-type", "multipart/form-data; boundary=" + boundary); } @override public boolean ischunked() { return false; } @override public boolean isrepeatable() { return false; } @override public boolean isstreaming() { return false; } @override public void writeto(final outputstream outstream) throws ioexception { outstream.write(out.tobytearray()); } @override public header getcontentencoding() { return null; } @override public void consumecontent() throws ioexception, unsupportedoperationexception { if (isstreaming()) { throw new unsupportedoperationexception( "streaming entity not implement #consumecontent()"); } } @override public inputstream getcontent() throws ioexception, unsupportedoperationexception { return new bytearrayinputstream(out.tobytearray()); } }
Comments
Post a Comment