java - How to send a json string as an input parameter to a POST request in android -
and facing problem in sending json string post request.
this url: http://172.25.183.183:8080/jiraservice/rest/runquery
key : query
value:
{ "jql": "project=<projectkey>", "startat": 0, "maxresults": 100, "fields": [ "summary", "customfield_10006", "status", "description" ] }
where <projectkey>
value stored in shared preferences please help
this code
try{ textview op=(textview) findviewbyid(r.id.resp); url url=new url("http://172.25.183.183:8080/jiraservice/rest/runquery"); httpsurlconnection conn=(httpsurlconnection)url.openconnection(); conn.setrequestmethod("post"); string projectkey=home.savedid; jsonobject jsonparam = new jsonobject(); jsonparam.put("query", " "{ \"jql\": \"project=" + projectkey + "\", \"startat\": 0, \"maxresults\": 100, \"fields\": [\"summary\",\"customfield_10006\", \"status\", \"description\"] }""); how send parameters?? conn.setdooutput(true); dataoutputstream dbstrm=new dataoutputstream(conn.getoutputstream()); dbstrm.flush(); dbstrm.close(); int respnse=conn.getresponsecode(); string output="request url"+url; output+=system.getproperty("line.separator"); output+=system.getproperty("line.separator")+"response code"+respnse; bufferedreader br=new bufferedreader(new inputstreamreader(conn.getinputstream())); string line= ""; stringbuilder respop=new stringbuilder(); while((line=br.readline())!=null){ respop.append(line); } br.close(); output +=system.getproperty("line.separator")+respop.tostring(); op.settext(output); }catch(malformedurlexception ae){ ae.printstacktrace(); }catch (ioexception e){ e.printstacktrace(); }
try{ url url=new url("http://172.25.183.183:8080/jiraservice/rest/runquery"); httpurlconnection conn=(httpurlconnection)url.openconnection(); conn.setrequestmethod("post"); conn.setdoinput(true); conn.setdooutput(true); string projectkey=home.savedid; list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("query"," { \"jql\": \"project=" + projectkey + "\", \"startat\": 0, \"maxresults\": 100, \"fields\": [\"summary\",\"customfield_10006\", \"status\", \"description\"] }")); outputstream os = conn.getoutputstream(); bufferedwriter writer = new bufferedwriter( new outputstreamwriter(os, "utf-8")); writer.write(getquery(params)); writer.flush(); writer.close(); os.close(); conn.connect(); int respnse=conn.getresponsecode(); string output="request url"+url; output+=system.getproperty("line.separator"); output+=system.getproperty("line.separator")+"response code"+respnse; bufferedreader br=new bufferedreader(new inputstreamreader(conn.getinputstream())); string line= ""; stringbuilder respop=new stringbuilder(); while((line=br.readline())!=null){ respop.append(line); } br.close(); output +=system.getproperty("line.separator")+"issuelist api"+respop.tostring(); system.out.println("issuelist"+ output); string respfield=resp(respop.tostring()); system.out.println(" response"+ respfield); }catch(malformedurlexception ae){ ae.printstacktrace(); }catch (ioexception e){ e.printstacktrace(); }
getquery() method
private string getquery(list<namevaluepair> params) throws unsupportedencodingexception { stringbuilder result = new stringbuilder(); boolean first = true; (namevaluepair pair : params) { if (first) first = false; else result.append("&"); result.append(urlencoder.encode(pair.getname(), "utf-8")); result.append("="); result.append(urlencoder.encode(pair.getvalue(), "utf-8")); } return result.tostring(); }
Comments
Post a Comment