java - Convert a Python POST request to Android -



java - Convert a Python POST request to Android -

i trying utilize illustration of python post api in android project. not have much experience httpsurlconnection think need utilize accomplish this. have looked @ tutorials not sure how integrate python code. python:

# highly suggested utilize requests bundle # http://www.python-requests.org/en/latest/ import requests # read in image , build payload image = open("example.jpg").read() info = {"api_key": "kh8hdoai0wrjb0lyea3emu5n4icwyoqo"} files = {"image": open("example.jpg")} # fire off request r = requests.post("http://www.idmypill.com/api/id/", info = data, files = files) # contents returned json string print r.content

this have far on trying convert android not sure @ if work or right way go this.

// http post request private void sendpost() throws exception { string url = "http://www.idmypill.com/api/id/"; url obj = new url(url); httpsurlconnection idmypill = (httpsurlconnection) obj.openconnection(); //add reuqest header idmypill.setrequestmethod("post"); string urlparameters = ""; // send post request idmypill.setdooutput(true); dataoutputstream wr = new dataoutputstream(idmypill.getoutputstream()); wr.writebytes(urlparameters); wr.flush(); wr.close(); int responsecode = idmypill.getresponsecode(); system.out.println("\nsending 'post' request url : " + url); system.out.println("post parameters : " + urlparameters); system.out.println("response code : " + responsecode); bufferedreader in = new bufferedreader( new inputstreamreader(idmypill.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); //print result system.out.println(response.tostring()); }

any advice much appreciated.

for rest webservices recommend using https://github.com/47deg/appsly-android-rest . has easy utilize api , don't have care lot backend part. hope want info in android app. if want translate method may not right :)

java android python post httpurlconnection

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -