Home java Getting data from the site in JSON format

Getting data from the site in JSON format

Author

Date

Category

How to make requests to receive in an application from the site in JSON format? How to find Links to JSON on the site? Need from the site http://openweathermap.org/ Receive data in JSON about the weather for the city selected in the attachment.


Answer 1, Authority 100%

Sendpospill according to the site documentation accordingly. JSON-request is formed as follows:

url url = new url ("http://youurrl.com");
HttpsurlConnection Conn = (httpsurlconnection) url.openconnection ();
Conn.SetreadTimeout (10,000);
Conn.setConnectTimeout (15000);
Conn.setRequestmethod ("POST");
CONN.SETDOINPUT (TRUE);
Conn.Setdooutput (True);
// Request design, for example {"auth": {"UserName": ADM, "Password": pwd}, "coord": {"x": 23, "y": 255}}
Jsonobject myjson = new jsonobject ();
Jsonobject auth = new jsonobject ();
Auth.put ("UserName", "Adm");
auth.put ("Password", "pwd");
JSONOBJECT COORDS = NEW JSONOBJECT ();
coords.put ("x", "23");
coords.put ("Y", "255");
MyJSon.put ("Auth", auth.tostring ());
myjson.put ("coords", coords.tostring ());
Con.setRequestProperty ("Content-Type", "Application / JSON; Charset = UTF-8");
// Display Query Result
OutputStreamwriter WR = New OutputStreamWriter (Con.getOutputstream ());
WR.WRITE (Myjson.tostring ());
wr.flush ();
STRINGBUILDER SB = new stringbuilder ();
int httpresult = con .GetResponsecode ();
if (httpresult == httpurlconnection.http_ok) {
  BufferedReader Br = New InputStreamReader (Con.getInputStream (), "UTF-8"));
  String Line = NULL;
  While ((Line = Br.ReadLine ())! = NULL) {
    SB.APPEND (LINE + "\ N");
  }
  Br.Close ();
  System.out.PrintLN ("" + sb.tostring ());
} else {
  System.Out.PrintLN (Con.GetResponsemessage ());
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions