java - 500 Internal server when trying to upload image to server -
i working on application allows user upload image server. getting 500 internal server error .i cant seem find related error solve problem. code follows:
class retreivefeedtask extends asynctask<string, void, string> { protected string doinbackground(string... url){ try { bytearrayoutputstream bos = new bytearrayoutputstream(); bitmapdrawable drawable = (bitmapdrawable) imageview.getdrawable(); bitmap bitmap = drawable.getbitmap(); bitmap.compress(compressformat.jpeg, 50, bos); byte[] data = bos.tobytearray(); httpclient httpclient = new defaulthttpclient(); httppost postrequest = new httppost("http://10.155.103.167:9090/restserver/rest/todos"); string filename = string.format("file_%d.jpg", new date().gettime()); bytearraybody bab = new bytearraybody(data, filename); contentbody mimepart = bab; // file file= new file("/mnt/sdcard/forest.png"); // filebody bin = new filebody(file); multipartentity reqentity = new multipartentity(httpmultipartmode.browser_compatible); reqentity.addpart("file", bab); postrequest.setentity(reqentity); postrequest.setheader("content-type", "application/json"); int timeoutconnection = 60000; httpparams httpparameters = new basichttpparams(); httpconnectionparams.setconnectiontimeout(httpparameters, timeoutconnection); int timeoutsocket = 60000; httpconnectionparams.setsotimeout(httpparameters, timeoutsocket); httpconnectionparams.settcpnodelay(httpparameters, true); httpresponse response = httpclient.execute(postrequest); bufferedreader reader = new bufferedreader(new inputstreamreader( response.getentity().getcontent(), "utf-8")); string sresponse; stringbuilder s = new stringbuilder(); system.out.println("response: " + response.getstatusline()); while ((sresponse = reader.readline()) != null) { s = s.append(sresponse); } txt.settext("new text"+s); } catch (exception e) { // handle exception here e.printstacktrace(); system.out.println(e.tostring()); } return null; } }
use code upload images it's working fine me
public class uploadtoserver extends asynctask<string, string, string>{ @override protected void onpreexecute() { super.onpreexecute(); } @override protected string doinbackground(string... args){ string status=""; string url = ""; try{ log.d("image path ======",takepicture.file.tostring()); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); file file = new file(takepicture.file.tostring()); filebody bin = new filebody(file); multipartentity reqentity = new multipartentity(); reqentity.addpart("content-disposition", new stringbody("form-data")); reqentity.addpart("name", new stringbody("test")); reqentity.addpart("filename", bin); reqentity.addpart("content-type", new stringbody("image/jpg")); httppost.setentity(reqentity); log.d("executing request ", httppost.getrequestline().tostring()); httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); if (resentity != null) { log.d("response content length: ",resentity.getcontentlength()+""); if(resentity.getcontentlength()>0) { status= entityutils.tostring(resentity); } else { status= "no response server"; log.d("status----->",status); } } else { status = "no response server"; log.d("status----->",status); } } catch (exception e) { e.printstacktrace(); status = "unable connect server"; } return status; } @override protected void onpostexecute(string status) { super.onpostexecute(status); } }
Comments
Post a Comment