java - Uploading Multipart-form-data to php -
i upload data (username, description,..) , photo android-app server (php-script).
by searching found solution: how take photo , send http post request android?
by click on send button, upload-process starts , php-script works $_post -vars empty? can me? request-code:
[...] case r.id.btnsend: string strname = tname.gettext().tostring(); string stremail = temail.gettext().tostring(); string strdatum = tdatum.gettext().tostring(); string strbeschreibung = tbeschreibung.gettext().tostring(); nvpairs = new arraylist<namevaluepair>(2); nvpairs.add(new basicnamevaluepair("sender", "andoidapp")); nvpairs.add(new basicnamevaluepair("name", strname)); nvpairs.add(new basicnamevaluepair("email", stremail)); nvpairs.add(new basicnamevaluepair("datum", strdatum)); nvpairs.add(new basicnamevaluepair("beschreibung", strbeschreibung)); nvpairs.add(new basicnamevaluepair("bild", bmpurl)); new uploadtask().execute(); } [...] private class uploadtask extends asynctask<void, void, list<string>> { @override protected list<string> doinbackground(void... params) { string response = ""; try { response = new multipartserver().postdata(new url(posturl), nvpairs); } catch (ioexception e) { e.printstacktrace(); } return null; } }
on multipartserver-code changed line:
if ((avatarname = pair.getname()).equals("avatar")) { avatarpath = pair.getvalue(); }
to this:
if ((avatarname = pair.getname()).equals("bild")) { avatarpath = pair.getvalue(); }
and variables "bmpurl" , "posturl" set correctly @ line in code.
thanks help! found solution problem. problem @ script how take photo , send http post request android?
i changed this:
public class multipartserver { private static final string tag = "multipartserver"; private static final string crlf = "\r\n"; private static final string twohyphens = "--"; private static final string boundary = "aab03x"; //***** private string avatarname = null; private string avatarpath = null; public string postdata(url url, list<namevaluepair> namevaluepairs) throws ioexception { httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setreadtimeout(10000); connection.setconnecttimeout(15000); connection.setrequestmethod("post"); connection.setusecaches(false); connection.setdoinput(true); connection.setdooutput(true); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("cache-control", "no-cache"); connection.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); fileinputstream inputstream; outputstream outputstream = connection.getoutputstream(); dataoutputstream dataoutputstream = new dataoutputstream(outputstream); (namevaluepair pair : namevaluepairs) { dataoutputstream.writebytes(crlf); dataoutputstream.writebytes(twohyphens + boundary + crlf); dataoutputstream.writebytes( "content-disposition: form-data; name=\"" + urlencoder.encode(pair.getname(), "utf-8") + "\";" + crlf); dataoutputstream.writebytes(crlf); dataoutputstream.writebytes(urlencoder.encode(pair.getvalue(), "utf-8")); if (pair.getname().equals("bild")) { avatarname = pair.getname(); avatarpath = pair.getvalue(); } } // write avatar (if any) if (avatarname != null && avatarpath != null) { dataoutputstream.writebytes(crlf); dataoutputstream.writebytes(twohyphens + boundary + crlf); dataoutputstream .writebytes("content-disposition: form-data; name=\"" + avatarname + "\";filename=\"" + new file(avatarpath).getname() + "\";" + crlf); dataoutputstream.writebytes(crlf); inputstream = new fileinputstream(avatarpath); byte[] data = new byte[1024]; int read; while ((read = inputstream.read(data)) != -1) dataoutputstream.write(data, 0, read); inputstream.close(); dataoutputstream.writebytes(crlf); dataoutputstream.writebytes(twohyphens + boundary + twohyphens + crlf); } dataoutputstream.flush(); dataoutputstream.close(); string responsemessage = connection.getresponsemessage(); log.d(tag, responsemessage); inputstream in = connection.getinputstream(); bufferedreader bufferedreader = new bufferedreader( new inputstreamreader(in, "utf-8")); stringbuilder response = new stringbuilder(); char[] b = new char[512]; int read; while ((read = bufferedreader.read(b)) != -1) { response.append(b, 0, read); } connection.disconnect(); log.d(tag, response.tostring()); return response.tostring(); } }
Comments
Post a Comment