Java tutorial
// Copyright (c) 2015 Cybozu,Inc. // Released under the MIT license package com.cybozu.kintone.gpsPunch; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import net.arnx.jsonic.JSON; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.apache.http.Header; public class GpsPunchRequestManager { /* --- PROPERTY --- */ String authorization = ""; String target = ""; String companyId = ""; HttpClient httpClient = null; /* --- CONSTRUCTOR --- */ public GpsPunchRequestManager() { // ??? try { ResourceBundle rb = ResourceBundle.getBundle("gpspunch"); authorization = rb.getString("authorization"); companyId = rb.getString("companyid"); target = rb.getString("target"); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // HTTP Client?? List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("Authorization", authorization)); // HTTP Client?? this.httpClient = HttpClientBuilder.create().setDefaultHeaders(headers).build(); } /* --- METHOD --- */ public String addSpot(long id, String companyName, String address, String tel) { String endPoint = "spots?"; String parm = ""; int responseStatus = 0; String body = ""; String spotId = ""; // ???? try { parm = "company_id=" + URLEncoder.encode(companyId, "utf-8") + "&spot_code=" + String.valueOf(id) + "&spot_name=" + URLEncoder.encode(companyName, "utf-8") + "&address=" + URLEncoder.encode(address, "utf-8") + "&tel=" + URLEncoder.encode(tel, "utf-8"); } catch (Exception e) { e.printStackTrace(); System.exit(1); } // ?? try { // POST HttpPost httpPost = new HttpPost(target + endPoint + parm); HttpResponse response = httpClient.execute(httpPost); responseStatus = response.getStatusLine().getStatusCode(); body = EntityUtils.toString(response.getEntity(), "UTF-8"); // ?200???????ID? if (responseStatus == 200) { Map map = JSON.decode(body, HashMap.class); spotId = (String) map.get("spot_id"); } else { System.out.println( "??????????"); System.exit(1); } } catch (Exception e) { System.out.println(responseStatus); Map map = JSON.decode(body, HashMap.class); String err = map.toString(); System.out.println(err); e.printStackTrace(); } return spotId; } // addSpot } // class