android - Unable to write the parameters to url using HttpURLConnection -


i'm trying post http url parameters. i've appended parameters using appendqueryprameters statements after build() skipped , control comes out of asynctask.below th snippet of asynctask

private class myasynctask extends asynctask<string, integer, string> {         @override         protected string doinbackground(string... params) {             // todo auto-generated method stub               string givendob = params[0];             string givensurname = params[1];             string givencaptcha = params[2];             string response = "";             try {                 uri.builder builder = new uri.builder()                         .appendqueryparameter("dateofbirth", givendob)                         .appendqueryparameter("usernamedetails.surname", givensurname)                         .appendqueryparameter("captchacode", givencaptcha);                 string query = builder.build().tostring();                 printwriter out = new printwriter(connection.getoutputstream());                 out.print(query);                 out.close();                 int responsecode = connection.getresponsecode();                 log.d("responsecode", string.valueof(responsecode));     /*             bufferedwriter writer = new bufferedwriter(                       new outputstreamwriter(connection.getoutputstream(), "iso-8859-1"));                  writer.write(query);                writer.flush();                writer.close();     */                 connection.getoutputstream().close();                 if (responsecode == httpsurlconnection.http_ok) {                     string line;                     bufferedreader br = new bufferedreader(new inputstreamreader(connection.getinputstream()));                     while ((line = br.readline()) != null) {                         response += line;                          log.d("response", response);                     }                 } else {                     response = "";                 }             } catch (ioexception e) {                 e.printstacktrace();             }               return response;         }          @override         protected void onpostexecute(string s) {             log.d("res", s);         }      } 

i tried printwriter also.still skips execution of statements after line string query = builder.build().tostring();

ps: i've opened httpurlconnection in asynctask , calling on oncreate() below code.

 url url = new url("https://myurl.com/path1/path2/path3.html");             connection = (httpsurlconnection) url.openconnection();             connection.setreadtimeout(10000);             connection.setconnecttimeout(15000);             connection.setrequestmethod("post");             connection.setdoinput(true);             connection.setdooutput(true); 

used this reference

i tell send parameters server using httpurlconnection object:

            // instantiate connexion.             url url = new url(_url);             httpurlconnection con;               // build data string send server:             string data = stringutils.paramstourlstring(params);              /* obtain new httpurlconnection calling url.openconnection() , casting result httpurlconnection.*/             con = (httpurlconnection)url.openconnection();             // activar método post:             // instances must configured setdooutput(true) if include request body.             con.setdooutput(true);             // data size known:             con.setfixedlengthstreamingmode(data.getbytes("utf-8").length);             // establecer application/x-www-form-urlencoded debido la simplicidad de los datos             //con.setrequestproperty("content-type", "application/x-www-form-urlencoded"); // no sirve para utf-8             con.setrequestproperty("accept-charset", "utf-8");             con.getcontentencoding();              // set time out both reading , writing operations.             con.setconnecttimeout(30*1000);             con.setreadtimeout(30*1000);              // read response:             // upload request body: write data on output stream (towards server)             outputstream out = new bufferedoutputstream(con.getoutputstream());             out.write(data.getbytes("utf-8"));             out.flush();             out.close();              // store input stream (server response):             // if response has no body, method returns empty stream.             = new bufferedinputstream(con.getinputstream());              // return json object.             jobj = castresponsetojson(is);              // disconnect: release resources.             con.disconnect(); 

and stringutils.paramstourlstring(params) method converts parameters suitable url string:

/**  * method receives contentvalues container parameter  * , returns formed string send parameter throw hppt.  *  * @param params parameter send server.  * @return param1=param1value&param2=param2value&....paramx=paramxvalue.  */ public static string paramstourlstring (contentvalues params) {      string data = "";      set<map.entry<string, object>> s = params.valueset();     iterator itr = s.iterator();      log.d("constructing url", "contentvalue length : " + params.size());      while(itr.hasnext())     {         map.entry me = (map.entry)itr.next();         string key = me.getkey().tostring();         string value =  me.getvalue().tostring();          try {             data+=(urlencoder.encode(key, "utf-8")+"="+urlencoder.encode(value, "utf-8")+"&");         } catch (unsupportedencodingexception e) {             e.printstacktrace();         }     }      // removing last char data:     return (data.substring(0, data.length()-1)); } 

the parameters received paramstourlstring(params) method must contained on contentvalues object this:

    contentvalues params = new contentvalues();     params.put("param1name", "param1value");     params.put("param2name", "param2value");     params.put("param3name", "param3value"); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -