Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

To pass JSON data in POST method using HTTPURLconnection

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

i am trying to use an API for Reprojecting Map coordinates from WGS84 to BNG (British National Grid) in java code.

I am trying to pass JSON data in URL, while API is working correctly with my input JSON data, it is not working through my java code.

Following is my code:


URL url = new URL(
"http://********:8080/v1/reproject/from/WGS84/to/BNG";);

                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();


conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

// Create Json objects then convert it to String to send to URL

                    // Using static JSON string for now

String input = "{ \"type\": \"Point\",\"coordinates\":[526775.201,136760.255]}";

// setting up send
OutputStream os = conn.getOutputStream();

// Copy content 'JSON' into
os.write(input.getBytes());
System.out.println("Os output stream" + os.toString());

// clean up
os.flush();

os.close();



if (conn.getResponseCode() != 200) {
System.out.println("Bad Request");
}

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();

}
catch (MalformedURLException e1) {
e1.printStackTrace();
}

catch (IOException e) {

e.printStackTrace();
}

}
}

It is giving me output:

Os output stream{ "type": "Point","coordinates":[526775.201,136760.255]}
conn.responseCode()200
Output from Server ....

{"type":"Point","coordinates":[0.0,0.0]}
 
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's the expected output?
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{
 "type": "Point",
 "coordinates": [
   -0.19011891989,
   51.116112794309
 ]
}

This output I am getting while using API, not through Java code
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Means the problem might be with your server code, because client is just reading the input from server and rendering it.
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my code is not appending Json String to URL.


Have pasted my code.
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using POST , you don't need to append it to the url.
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but then how it will produce expected output, as it needs json data.

Still, not able to figure out, why it's not giving expected results.
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For that we probably need to look at the server code.
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is output from API which I am trying to use in Java code:

Request_url.PNG
[Thumbnail for Request_url.PNG]
curlRequest.PNG
[Thumbnail for curlRequest.PNG]
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't have server code, I just have web service, which I have to use in my java code.
Web service API results are above I have pasted.
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your client code is just rendering the output received from the web service with a specific request sent.  As the output is generated from the web service, you probably have to look at that part, because client is just rendering the output.  
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for giving your time but my Client is not rendering expected output from webservice.

This is my issue.

I need same output as WS API , in my java code to ensure, WS is being called correctly by my client code
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean to say the returned co-ordinate values are wrong or anything is wrong with the returned format?
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
Actual output at client:
{"type":"Point","coordinates":[0.0,0.0]}

Expected:

Post Today 2:44:03 PM     Subject: To pass JSON data in POST method using HTTPURLconnection
{
 "type": "Point",
 "coordinates": [
   -0.19011891989,
   51.116112794309
 ]
}

 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show how you are making the api call directly not from java code.
 
Swastik Dey
Rancher
Posts: 2265
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my understanding the value should be passed into some parameter variable.  For example if you call it through get it should be some what like this

<the url>?paramname=>thejsonvalue

similarly if is post

it should be some what like

String input = "paramname={ \"type\": \"Point\",\"coordinates\":[526775.201,136760.255]}";
 
Rancher
Posts: 4607
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends what the server is expecting.
The current payload for the POST is perfectly valid JSON, and a perfectly valid payload.
 
Himani Bhatia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it resolved :-)

My crs (coordinate system) names BNG, WGS84 were not correct in URL,  I was doing it as :

http://cbcarcgis.int.crawley.gov.uk:8080/v1/reproject/from/WGS84/to/BNG and passing BNG values to be reprojected.  

While it should be:

http://cbcarcgis.int.crawley.gov.uk:8080/v1/reproject/from/BNG/to/WGS84

Where BNG is British National Grid system (Eastings and Northings) and WGS84 is longitude and latitude system.

Thanks everyone for help
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic