-3

i want to make an application which is based on a php page, sending to it parameters to obtain data from the remote db through the php page.

I will use jsons, but my question is:

assumed i use this code to send request:

URL url = new URL("http://www.francescorizzi.altervista.org/Main.php?Request=Login&UserName=mario&Password=lol");
URLConnection conn = url.openConnection();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;

while ((line = rd.readLine()) != null) {
    System.out.println(line);
}

Should i recreate the url object, conn, and rd at each new request? or is there a more efficient way to do this?

Please note: I'm new to http handling through java.

Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
Francesco Rizzi
  • 621
  • 4
  • 19
  • do you want to build the app with java or with php? – monterico Jun 10 '16 at 09:36
  • 1
    Possible duplicate of [Using java.net.URLConnection to fire and handle HTTP requests](http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests) – B001ᛦ Jun 10 '16 at 09:36
  • @monterico The app is in java, the data are retrieved through php. – Francesco Rizzi Jun 10 '16 at 09:42
  • @bub Thanks for your answer, i noticed that doing multiple url.openConnection() is fine, so it's not a heavy process. But what about the input stream? should i reinitialize it after each openConnection() call? – Francesco Rizzi Jun 10 '16 at 09:43
  • 2
    @Francesco Rizzi : yes, you have to create a new reader for each request, since you get a new inputstream each time. – Arnaud Jun 10 '16 at 09:44
  • when you say "I will use jsons", do you mean that the result is in JSON format? – Nicolas Filotto Jun 10 '16 at 09:59
  • @NicolasFilotto yes, i mean that. – Francesco Rizzi Jun 10 '16 at 20:57
  • @Berger unfortunately, doing all the stuff you suggested makes my session to get closed... I need to store session data for the whole process, which encloses the multiple GET requests. is there a way to do so? thanks – Francesco Rizzi Jun 10 '16 at 20:57

1 Answers1

1

since it is an http request and you retrieving data from that particular url, there is a need to recreate the object because the site may get updated later. so creating new object again will dispose the previous object reference and also close connection after retrieving data.

abhishek
  • 291
  • 3
  • 26