Java/Excercise

dataStructure ch02 HttpMainTest2

낭구리 2021. 9. 14. 17:49
package ch02;

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

import com.google.gson.Gson;

public class HttpMainTest2 {
	public static void main(String[] args) {
//실무 ****
		try {
			URL url = new URL("https://jsonplaceholder.typicode.com/todos/10");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 연결메서드
			// HttpURLConnection 대문자이기때문에 클래스

			connection.setRequestMethod("GET"); // <>post(공개) get(비밀)
			connection.setRequestProperty("Content-type", "application/json"); // json타입으로 부르기위해
			connection.connect();

			int statusCode = connection.getResponseCode(); // 잘못나온다면 404라는 코드를 내보내게된다.(주소가잘못될경우)
			System.out.println("statusCode:" + statusCode); // 정상 작동하여 200이라는 코드를 내보내게됨.

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			StringBuffer sb = new StringBuffer();
			// String만 쓰면 메모리를 계속사용해서 스트링버퍼사용
			// String만쓰면 객체가 계속생성
			String line = null;

			if (statusCode == 200) {
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
			} else {
				System.out.println("서버에 연결 할 수 없습니다.");
			}

			String str = sb.toString(); // 문자열로 변환
			Gson gson = new Gson();
			Todo todo = gson.fromJson(str, Todo.class); //앞에는 변수 넣고 뒤ㅏ에는 class
			System.out.println(todo.id);
			System.out.println(todo.title);
			System.out.println(todo.userId);
			System.out.println(todo.completed);
			
			
			
//			System.out.println(str);
//			System.out.println("----");
			//인덱스번호로 무엇이있는지 확인하는 과정
//			System.out.println(str.substring(4, 10)); //userId
//			System.out.println(str.substring(13, 14)); //1
//			
//			Todo todo = new Todo();
//			todo.id = str.substring(13, 14); //형변환도해야됨
			

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
}
package ch02;

//DAO data access object 
//정보를 담는 과정
public class Todo {

	int userId;
	int id;
	String title;
	String completed;
	
}

 

 

package ch02;

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

import com.google.gson.Gson;

public class HttpMainTest3 {
	public static void main(String[] args) {
//실무 ****
		try {
			URL url = new URL("https://jsonplaceholder.typicode.com/todos/10");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 연결메서드
			// HttpURLConnection 대문자이기때문에 클래스

			connection.setRequestMethod("GET"); // <>post(공개) get(비밀)
			connection.setRequestProperty("Content-type", "application/json"); // json타입으로 부르기위해
			connection.connect();

			int statusCode = connection.getResponseCode(); // 잘못나온다면 404라는 코드를 내보내게된다.(주소가잘못될경우)
			System.out.println("statusCode:" + statusCode); // 정상 작동하여 200이라는 코드를 내보내게됨.

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			StringBuffer sb = new StringBuffer();
			// String만 쓰면 메모리를 계속사용해서 스트링버퍼사용
			// String만쓰면 객체가 계속생성
			String line = null;

			if (statusCode == 200) {
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
			} else {
				System.out.println("서버에 연결 할 수 없습니다.");
			}

			String str = sb.toString(); // 문자열로 변환
			Gson gson = new Gson();
			Post post = gson.fromJson(str, Post.class);
//			Todo todo = gson.fromJson(str, Todo.class); //앞에는 변수 넣고 뒤ㅏ에는 class
			System.out.println(post.userId);
			System.out.println(post.id);
			System.out.println(post.title);
			System.out.println(post.body);
//			
			
			
//			System.out.println(str);
//			System.out.println("----");
			//인덱스번호로 무엇이있는지 확인하는 과정
//			System.out.println(str.substring(4, 10)); //userId
//			System.out.println(str.substring(13, 14)); //1
//			
//			Todo todo = new Todo();
//			todo.id = str.substring(13, 14); //형변환도해야됨
			

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
}

 

package ch02;

public class Post {

	int userId;
	int id;
	String title;
	String body;
}

 

package ch02;

import java.io.BufferedReader;

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

import com.google.gson.Gson;

public class HttpMainTest4 {
	public static void main(String[] args) {
//실무 ****
		try {
			URL url = new URL("https://jsonplaceholder.typicode.com/comments/100");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 연결메서드
			// HttpURLConnection 대문자이기때문에 클래스

			connection.setRequestMethod("GET"); // <>post(공개) get(비밀)
			connection.setRequestProperty("Content-type", "application/json"); // json타입으로 부르기위해
			connection.connect();

			int statusCode = connection.getResponseCode(); // 잘못나온다면 404라는 코드를 내보내게된다.(주소가잘못될경우)
			System.out.println("statusCode:" + statusCode); // 정상 작동하여 200이라는 코드를 내보내게됨.

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			StringBuffer sb = new StringBuffer();
			// String만 쓰면 메모리를 계속사용해서 스트링버퍼사용
			// String만쓰면 객체가 계속생성
			String line = null;

			if (statusCode == 200) {
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
			} else {
				System.out.println("서버에 연결 할 수 없습니다.");
			}

			String str = sb.toString(); // 문자열로 변환
			Gson gson = new Gson();
			Comments comments = gson.fromJson(str, Comments.class);
//			Post post = gson.fromJson(str, Post.class);
//			Todo todo = gson.fromJson(str, Todo.class); //앞에는 변수 넣고 뒤ㅏ에는 class
			System.out.println(comments.postId);
			System.out.println(comments.id);
			System.out.println(comments.name);
			System.out.println(comments.email);
			System.out.println(comments.body);
			
			
			
//			System.out.println(str);
//			System.out.println("----");
			//인덱스번호로 무엇이있는지 확인하는 과정
//			System.out.println(str.substring(4, 10)); //userId
//			System.out.println(str.substring(13, 14)); //1
//			
//			Todo todo = new Todo();
//			todo.id = str.substring(13, 14); //형변환도해야됨
			

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
}

 

package ch02;

public class Comments {

	int postId;
	int id;
	String name;
	String email;
	String body;
}

'Java > Excercise' 카테고리의 다른 글

dataStructure ch02 HttpMainTest5  (0) 2021.09.14
dataStructure ch02 HttpMainTest1  (0) 2021.09.14
dataStructure ch02 JSON이란?  (0) 2021.09.14
dataStructure ch01 DataStructureTest3  (0) 2021.09.14
dataStructure ch01 DataStructureTest2  (0) 2021.09.14