Page 70 - การโปรแกรมเว็บ
P. 70

12-58 การโปรแกรมเว็บ

ตาราง (ต่อ)

InfoServiceClient.java

19 		// Step1: อา่ นข้อมลู เจสันจากไฟล์
20 		// เปลย่ี นทีเ่ ก็บไฟล์ infoJSON.txt path ท่ีบรรทัดน้ี
21 InputStream infoInputStream = new FileInputStream("C:\\infoJSON.txt");
22 InputStreamReader infoReader = new InputStreamReader(infoInputStream);
23 BufferedReader br = new BufferedReader(infoReader);
24 		String line;
25
26 		while ((line = br.readLine()) != null) {
27 	                    string += line + "\n";
28 		}
29
30 		JSONObject jsonObject = new JSONObject(string);
31 		System.out.println(jsonObject);
32
33 		// Step2: สง่ ขอ้ มลู เจสนั ท่อี ่านไดไ้ ปใหเ้ ซอร์วิส
34
35 		try {
36 URL url = new URL("http://localhost:8080/Infoapi/api/infoService");
37 URLConnection connection = url.openConnection();
38 connection.setDoOutput(true);
39 connection.setRequestProperty("Content-Type", "application/json");
40 connection.setConnectTimeout(5000);
41 connection.setReadTimeout(5000);
42
43 OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
44 	                    out.write(jsonObject.toString());
45 	                    out.close();
46
47 BufferedReader in = new BufferedReader(new
48 InputStreamReader(connection.getInputStream()));
49
   65   66   67   68   69   70   71   72   73   74   75