java - Android HttpURLConnection write post data -


i'm trying send post data through httpurlconnection , doesn't seem send post data.

i'm able output data through toast before trying send post request, when check on server side makes connects php file, post data empty.

so, know data exists in jsonobject. know makes try catch statement, when connect it's not being sent or received properly.

try {         url url = new url(emailurl);         httpurlconnection conn = (httpurlconnection) url.openconnection();          conn.setdooutput(true);          conn.setrequestmethod( "post" );         conn.setrequestproperty( "content-type", "application/json");         conn.setrequestproperty( "charset", "utf-8");         conn.setrequestproperty( "content-length", integer.tostring( jsonobject.tostring().length() ));         conn.setusecaches( false );          outputstreamwriter wr = new outputstreamwriter(conn.getoutputstream());          wr.write(jsonobject.tostring());            wr.close();          bufferedreader reader = null;         reader = new bufferedreader(new inputstreamreader(conn.getinputstream()));         stringbuilder sb = new stringbuilder();         string line = null;          // read server response         while ((line = reader.readline()) != null) {             // append server response in string             sb.append(line + "\n");         }          string text = "";         text = sb.tostring();        } catch (exception e) {          throw new runtimeexception(e);     } 

try adding method make sure connect server.

modify code :

    int responsecodefromserver = -1;     try {          responsecodefromserver=conn.getresponsecode();     } catch (exception e){          e.printstacktrace();     }      if (responsecodefromserver == httpsurlconnection.http_ok){          bufferedreader reader = null;          reader = new bufferedreader(new inputstreamreader(conn.getinputstream()));          stringbuilder sb = new stringbuilder();         string line = null;         // read server response        while ((line = reader.readline()) != null) {           // append server response in string           sb.append(line + "\n");        }         string text = "";        text = sb.tostring();     } 

so if there's wrong while connecting servers see exception.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -