Http POST works on std Java does not work correctly on Android -
i banging head on next code works on std java command line not work in android on galaxy nexus running 4.2. i'm including 2 variations of attempting perform post http://cleverbot.com/webservicemin. original post logic taken chatterbot api revised logic effort utilize different http apis available in java. naive effort @ including chatterbot api in test project. original works main app on cmd line using standard java while neither version work in android.
private static string revisedpostlogic(string url, map<string, string> parameters) throws ioexception { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(parameters.size()); for(map.entry<string,string>eachentry : parameters.entryset()) namevaluepairs.add(new basicnamevaluepair(eachentry.getkey(),eachentry.getvalue())); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); bytearrayoutputstream rawresponse = new bytearrayoutputstream(); response.getentity().writeto(rawresponse); homecoming rawresponse.tostring(); } private static string old_post_logic(string url, map<string, string> parameters) throws exception { urlconnection connection = new url(url).openconnection(); ((httpurlconnection) connection).setrequestmethod("post"); connection.setdooutput(true); connection.setdoinput(true); outputstreamwriter osw = new outputstreamwriter(connection.getoutputstream()); osw.write(parameterstowwwformurlencoded(parameters)); osw.flush(); osw.close(); reader r = new bufferedreader(new inputstreamreader(connection.getinputstream())); stringwriter w = new stringwriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); homecoming w.tostring(); }
with revised logic no response server exception , original logic eofexception.
update
i tried running android , desktop java , discrepancy:
new java.net.url("http://cleverbot.com/webservicemin").openconnection().getresponsecode()
on desktop java response code 200. (i haven't tried reading stream i'm guessing homecoming text "denied") on android "eofexception". on desktop java (using groovy shortcut now), dump response code, headers , response content , get:
response code: 200 headers: [null:[http/1.1 200 ok], sc:[6870], rps:[server52054], mime-version:[1.0], content-length:[29], expires:[mon, 18 feb 2013 20:07:20 gmt], set-cookie:[rps=server52054, jsessionid=6c765c93a085f67d69f800d9eb76646c; path=/], connection:[keep-alive], server:[jabberwacky], rlm:[2416], cache-control:[no-cache;no-store], pragma:[no-cache], date:[mon, 18 feb 2013 19:56:42 gmt], content-type:[text/html;charset=iso-8859-1]] response: denied
again cannot passed response code if seek same on android. can connect root url without path without issue, it's when effort nail path off of root url when eofexception either or post. i've tried moving outputstreamwriter close() phone call toward end can read response code before closing output stream. still eofexception in case. (i've learned experience closing outputstream prematurely in mobile app can shut downwards entire connection.)
java android http
No comments:
Post a Comment