java - Sending JSON request to hipchat -
i making hipchat room work have send json request of:
post /v1/rooms/message?format=json&auth_token=token http/1.1 host: api.hipchat.com content-type: application/x-www-form-urlencoded content-length: 138 room_id=10&from=alerts&message=a+new+user+signed+up
so far have this:
public static void send(string send){ url url = null; httpurlconnection conn = null; try{ url = new url("http://api.hipchat.com"); conn = (httpurlconnection)url.openconnection(); conn.setrequestmethod("post"); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); conn.setrequestproperty("content-length", "138"); conn.setusecaches (false); conn.setdoinput(true); conn.setdooutput(true); dataoutputstream wr = new dataoutputstream( conn.getoutputstream ()); wr.writebytes (send); wr.flush (); wr.close (); inputstream = conn.getinputstream(); bufferedreader rd = new bufferedreader(new inputstreamreader(is)); string line; stringbuffer response = new stringbuffer(); while((line = rd.readline()) != null) { response.append(line); response.append('\r'); } rd.close(); system.out.println(line); }catch(exception e){ e.printstacktrace(); }finally{ if(conn != null) { conn.disconnect(); } } }
but in console returns null. how go sending above json request?
thanks
every time loop here
while((line = rd.readline()) != null) {
your line
variable replaced value returned rd.readline()
. last time loops, method call return null
. that's why line
null
.
i'm going assume wanted print out response
.
Comments
Post a Comment