hi im trying working image upload on android, im trying upload image , parse json data returned. when ever try pass data in either 500 error or json saying invalid user. cant server side code on clinets end , not release me or give me access. im going insane ive looked @ says should work, own sanity there ive missed or done wrong here passing login details incorrectly or what? fresh pair of eyes extremely appreciated here thanks.
class postimage extends asynctask<void, void, void> { @override protected void doinbackground(void... params) { log.i("api handler", "beginning image upload."); string crlf = "\r\n"; string twohyphens = "--"; string boundary = "*****"; try { bytearrayoutputstream bos = new bytearrayoutputstream(); //compress image jpeg format make file small enough. image.compress(bitmap.compressformat.jpeg, 90, bos); byte[] data = bos.tobytearray(); string charset = "utf-8"; //open http connection url url = new url(utilities.uploadimageapi); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.addrequestproperty(urlencoder.encode("username", charset), urlencoder.encode("username", charset)); conn.addrequestproperty(urlencoder.encode("password", charset), urlencoder.encode("password", charset)); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "image/jpeg"); conn.setrequestproperty("connection", "keep-alive"); conn.setdoinput(true); conn.setdooutput(true); //conn.setrequestproperty("content-disposition", "form-statsdataarray; name=name; filename=" + filename +".jpg"); dataoutputstream dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + crlf); dos.writebytes("content-disposition; form-statsdataarray; name=name" + "; filename=" + filename + ".jpeg"); dos.writebytes(crlf); dos.write(data); dos.writebytes(crlf); dos.writebytes(twohyphens + boundary + twohyphens + crlf); dos.flush(); dos.close(); if(conn.getresponsecode() == 200) { inputstream in = new bufferedinputstream(conn.getinputstream()); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder sb = new stringbuilder(); string line; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } log.i("api handler", sb.tostring()); } else { log.i("api handler", "bad response image upload " + conn.getresponsecode() + " message: " + conn.getresponsemessage()); //log.e("api handler", conn.geterrorstream().tostring()); inputstream = new bufferedinputstream(conn.geterrorstream()); //get error specifics bufferedreader reader = new bufferedreader(new inputstreamreader(i)); stringbuilder sb = new stringbuilder(); string line; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } { try { i.close(); } catch (ioexception e) { e.printstacktrace(); } } log.i("api handler", sb.tostring()); } } catch (exception e) { log.i("api handler", "upload failed"); log.e("api handler", "" + e.getmessage()); e.printstacktrace(); } return null; }
for obvious reasons ive changed login details being passed in generic username , password these become variables passed in @ later date once ive got working test account.
Comments
Post a Comment