Communicate with NodeJS server from Android native app

Here we are destined to create a Java RESTful client which will use Java build-in HTTP client library to communicate with nodejs server application. Here java.net.URL and java.net..HttpURLConnection are used to create GET and POST request.



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientGet {

 // http://localhost:8080/RESTfulExample/json/product/get
 public static void main(String[] args) {

   try {

  URL url = new URL("http://localhost:8080/RESTfulExample/json/product/get");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Accept", "application/json");

  if (conn.getResponseCode() != 200) {
   throw new RuntimeException("Failed : HTTP error code : "
     + conn.getResponseCode());
  }

  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 e) {

  e.printStackTrace();

   } catch (IOException e) {

  e.printStackTrace();

   }

 }

}

Share:

0 comments