java - Posting JSON data from txt file to URL -
i need post json data text file url.i have not come across such scenario.
i came across posting json data using httpurlconnection.
my json data : { "request": { "header": { "signature": "bhnums", "details": { "productcategorycode": "01", "specversion": "04" } }, "transaction": { "primaryaccountnumber": "8961060000402122658", "processingcode": "725400", "transactionamount": "000000000200", "transmissiondatetime": "150505141718", "systemtraceauditnumber": "035689", "localtransactiontime": "141718", "localtransactiondate": "150505", "merchantcategorycode": "0443", "pointofserviceentrymode": "021", "pointofserviceconditioncode":"00", "transactionfeeamount":"000000000200", "acquiringinstitutionidentifier": "10998156762", "track2data":";8961060000402122658=4912?", "retrievalreferencenumber": "44436440441", "merchantterminalid": "87654 987 ", "merchantidentifier": "10998156762", "merchantlocation": "688 pacific highwayyy chhhhatswood nswau", "transactioncurrencycode": "840", "additionaltxnfields": { "productid": "07675018955" , "externalaccountnumber":"353142040651369", } } } }
url :
http://10.18.141.12:30305/transaction/v/transactionw code : public static void main(string[] args) throws ioexception { try { url url = new url("http://10.38.141.32:30304/transaction/v2/transaction"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setdooutput(true); conn.setrequestmethod("get"); conn.setrequestproperty("content-type", "application/json"); string input = ""; outputstream os = conn.getoutputstream(); os.write(input.getbytes()); os.flush(); if (conn.getresponsecode() != httpurlconnection.http_created) { throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); } bufferedreader br = new bufferedreader(new inputstreamreader( (conn.getinputstream()))); string output; system.out.println("output server .... \n"); while ((output = br.readline()) != null) { system.out.println(output); } conn.disconnect(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
string input= "" ; // i need pass json data here not sure how pass it.
reading file , setting string able do. trial purpose had assign string input json data mentioned above, due qoutes , braces not able format string.
online resources explains data formated in format (string input="{\"qty\":100,\"name\":\"ipad 4\"}";
)
but have no idea how format such big json data manually.
i not sure if right, not able see expected output. new concept not able tweak code.
if me out of great help.
if have valid json text in file, not have parse yourself. however, if need check if json valid try writing parser choose parser world wide web. first step open file , read contents, so:
final stringbuilder sb = new stringbuilder(); final bufferedreader br = new bufferedreader(new filereader("your_file_name.txt")); string line; while ((line = br.readline()) != null) br.append(line); final string input = br.tostring(); // write string connection body // minimalize characters transfer json , jsonwriter doesn't format don't have spacelike characters in string
the rest have figured out ;)
Comments
Post a Comment