java - Http post get response Android -



java - Http post get response Android -

i want retrieve info external web page. when i'm navigating on , click show data, see developer console (under "network") http post phone call been making. if open it, can see info want retrieve android app , want string response.

but don't know how "build" http post request. code:

httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://portus.puertos.es/portus_rt/portusgwt/rpc"); httppost.addheader("connection", "keep-alive"); httppost.addheader("content-length", "172"); httppost.addheader("x-gwt-module-base", "http://portus.puertos.es/portus_rt/portusgwt/"); httppost.addheader("x-gwt-permutation", "3dede3a69cbbe62d4c3f58bf7278538f"); httppost.addheader("origin", "http://portus.puertos.es"); httppost.addheader("user-agent", "mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/37.0.2062.124 safari/537.36"); httppost.addheader("accept", "*/*"); httppost.addheader("referer", "http://portus.puertos.es/portus_rt/?locale=es"); httppost.addheader("accept-encoding", "gzip,deflate"); httppost.addheader("accept-language", "en-us,en;q=0.8,es;q=0.6,ca;q=0.4"); httppost.addheader("alexatoolbar-alx_ns_ph", "alexatoolbar/alxg-3.3"); httppost.addheader("content-type", "text/x-gwt-rpc; charset=utf-8"); httppost.addheader("host", "portus.puertos.es"); //i think need add together payload here list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("", "")); seek { httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); log.d("tag", entityutils.tostring(response.getentity())); } grab (exception e) { e.printstacktrace(); }

i don't know http protocols, don't know info of http post request (that can see developer console) need add. http post request see:

i'm not sure if need add together headers , how add together payload. not know if can this, grateful can guide me some

thanks!!

you can utilize function that, , convert string returned in jsonobject or whatever. adapt code if need it, think it's "almost" generic solution.

private string callserver(list<basicnamevaluepair> namevaluepairs,string path) { inputstream = null; stringbuilder sb = null; string result = null; httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(path); //your headers here //i'm afraid there much headers. seek cleaning , choosing neccessary ones. httppost.setheader("connection", "keep-alive"); httppost.setheader("content-length", "172"); httppost.setheader("x-gwt-module-base", "http://portus.puertos.es/portus_rt/portusgwt/"); httppost.setheader("x-gwt-permutation", "3dede3a69cbbe62d4c3f58bf7278538f"); httppost.setheader("origin", "http://portus.puertos.es"); httppost.setheader("user-agent", "mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/37.0.2062.124 safari/537.36"); httppost.setheader("accept", "*/*"); httppost.setheader("referer", "http://portus.puertos.es/portus_rt/?locale=es"); httppost.setheader("accept-encoding", "gzip,deflate"); httppost.setheader("accept-language", "en-us,en;q=0.8,es;q=0.6,ca;q=0.4"); httppost.setheader("alexatoolbar-alx_ns_ph", "alexatoolbar/alxg-3.3"); httppost.setheader("content-type", "text/x-gwt-rpc; charset=utf-8"); httppost.setheader("host", "portus.puertos.es"); seek { httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } grab (exception e1) { e1.printstacktrace(); } // convert response string seek { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); sb = new stringbuilder(); sb.append(reader.readline() + "\n"); string line = "0"; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } grab (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } homecoming result; }

even tough, recommend utilize component rest apis , http connections:

https://github.com/matessoftwaresolutions/androidhttprestservice

take , evaluate if worths you. allows manage "no connection", show/hide dialogs before , after phone call (or whatever) , more features.

i hope help you. ;)

java android http post

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -