Java tutorial
package com.culvereq.vimp.networking; import com.culvereq.vimp.models.ServiceRecord; import com.culvereq.vimp.models.TempServiceRecord; import com.culvereq.vimp.models.TempVehicle; import com.culvereq.vimp.models.Vehicle; import com.culvereq.vimp.util.Deserializer; import com.culvereq.vimp.util.Globals; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; public class ConnectionHandler { private final String serverAddress = "http://vimp.culvereq.com/"; private final int port = 80; private final String baseURL = serverAddress + (port != 80 ? ":" + port : ""); private final String vehicleURL = baseURL + "vehicle/"; private final String serviceURL = baseURL + "record/"; private final String loginURL = baseURL + "login/app"; public Vehicle getVehicle(int id) throws IOException, JSONException { URL requestURL = new URL(vehicleURL + id); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return new Deserializer().deserializeVehicle(response.toString()); } else { throw new IOException(); } } public ArrayList<Vehicle> getAllVehicles() throws IOException, JSONException { URL requestURL = new URL(vehicleURL + "all"); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>(); JSONObject json = new JSONObject(response.toString()); JSONArray vehiclesArrayJSON = json.getJSONArray("vehicles"); Deserializer deserializer = new Deserializer(); for (int i = 0; i < vehiclesArrayJSON.length(); i++) { JSONObject vehicleJSON = vehiclesArrayJSON.getJSONObject(i); Vehicle vehicle = deserializer.deserializeVehicle(vehicleJSON, vehicleJSON.toString()); vehicles.add(vehicle); } return vehicles; } else { throw new IOException(); } } public ServiceRecord getServiceRecord(int i) throws IOException, JSONException { URL requestURL = new URL(vehicleURL + "all"); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); return new Deserializer().deserializeService(json, response.toString()); } else { throw new IOException(); } } public ServiceRecord addServiceRecord(TempServiceRecord serviceRecord) throws IOException, JSONException { URL requestURL = new URL(serviceURL + "add"); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); String urlParameters = ""; urlParameters += "key=" + Globals.access_key; urlParameters += "&vehicle-id=" + serviceRecord.getParentVehicle().getId(); urlParameters += "&type-id=" + serviceRecord.getType().getValue(); urlParameters += "&service-desc=" + serviceRecord.getDescription(); urlParameters += "&service-date=" + serviceRecord.getServiceDate().getMillis() / 1000L; urlParameters += "&service-mileage=" + serviceRecord.getMileage(); connection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length); connection.setUseCaches(false); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(urlParameters); writer.flush(); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode == 201) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); return new Deserializer().deserializeService(json, response.toString()); } else if (responseCode == 400) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); throw new IOException(response.toString()); } else { throw new IOException("Got response code: " + responseCode); } } public Vehicle addVehicle(TempVehicle vehicle) throws IOException, JSONException { URL requestURL = new URL(vehicleURL + "add"); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); String urlParameters = ""; urlParameters += "key=" + Globals.access_key; urlParameters += "&vehicle-name=" + vehicle.getName(); urlParameters += "&vehicle-type-id=" + vehicle.getType().getValue(); urlParameters += "&vehicle-make=" + vehicle.getMake(); urlParameters += "&vehicle-model" + vehicle.getModel(); urlParameters += "&vehicle-year" + vehicle.getYear(); urlParameters += "&vehicle-mileage" + vehicle.getMileage(); urlParameters += "&vehicle-vin" + vehicle.getVin(); urlParameters += "&vehicle-license-no" + vehicle.getLicenseNumber(); connection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length); connection.setUseCaches(false); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(urlParameters); writer.flush(); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode == 201) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); return new Deserializer().deserializeVehicle(json, response.toString()); } else if (responseCode == 400) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); throw new IOException(response.toString()); } else { throw new IOException("Got response code: " + responseCode); } } public String login(String username, String password) throws IOException { URL requestURL = new URL(loginURL); HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection(); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); String urlParameters = ""; urlParameters += "username=" + username; urlParameters += "&password=" + password; connection.setRequestProperty("Content-Length", "" + urlParameters.getBytes().length); connection.setUseCaches(false); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(urlParameters); writer.flush(); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode == 200) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString().trim(); } else { return null; } } }