게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[질문][자바/안드]POST 요청 보낼 때 요청 본문 어케 보내야 하나요
게시물ID : programmer_5762짧은주소 복사하기
작성자 : 나데시코♥
추천 : 0
조회수 : 2655회
댓글수 : 10개
등록시간 : 2014/10/04 12:38:05
유투브 관련 안드로이드 어플 만드는 중입니다.

https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
이 사이트 참고해서 업로드 기능을 넣는 중입니당.
(업로드 기능이 꼭 필요하다기 보단....걍 공부하는 셈 치고 넣는 중입니당)

업로드할 동영상의 메타데이터를 POST 요청으로 보내려고 하는데요

제목 없음.png
요렇게 생겨먹은 POST 를 보내야 해요.
그래서 만든 메소드가....
(아, private static final String INITIAL_UPLOAD_URL = "https://www.googleapis.com/upload/youtube/v3/videos";  라고 클래스 맨 위에 정했어욤);
private String uploadMetaData(String filePath, boolean retry) throws IOException { 	String title = getTitleText(); 	String description = getDescriptionText(); 	String category = DEFAULT_VIDEO_CATEGORY; 	String tags = DEFAULT_VIDEO_TAGS; 	if (getTagsText() != null && getTagsText().trim().length() != 0) { 		tags = getTagsText(); 	} 		 	String urlData = "uploadType=resumable&part=snippet,status,contentDetails"; 	String uploadUrl = INITIAL_UPLOAD_URL + "?" + urlData; 		 	URL url = new URL(uploadUrl); 	HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 	urlConnection.setRequestMethod("POST"); 	urlConnection.setRequestProperty("Host", "www.googleapis.com"); 	urlConnection.setRequestProperty("Authorization", String.format("Bearer %s", accessToken)); 	urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 	urlConnection.setRequestProperty("X-Upload-Content-Type", "video/*"); 	// urlConnection.setRequestProperty("Content-Length", );	// urlConnection.setRequestProperty("X-Upload-Content-Length", );	urlConnection.setDoOutput(true); 		 		 	String postDataForm = "{\n\"snippet\": {\n\"title\": \"%s\",\n\"description\": \"%s\",\n\"tags\": "  			+ "[\"cool\", \"video\", \"more keywords\"],\n\"categoryId\": 22\n},\n\"status\": "  			+ "{\n\"privacyStatus\": \"public\",\n\"embeddable\": True,\n\"license\": \"youtube\"\n}\n}"; 	String postData = String.format(postDataForm, title, description, tags); 	DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); 	dos.writeBytes(postData); 	dos.flush(); 	dos.close();  	int responseCode = urlConnection.getResponseCode(); 	if (responseCode < 200 || responseCode >= 300) { 		// The response code is 40X		if ((responseCode + "").startsWith("4") && retry) { 			Log.d(LOG_TAG, "retrying to fetch auth token for " + name); 		//	this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken);			// Try again with fresh token			return uploadMetaData(filePath, false); 		} else { 			throw new IOException(String.format("response code='%s' (code %d)" + " for %s", 					urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); 		} 	}  	Log.d("크르릉", "urlConnection.getHeaderField " + urlConnection.getHeaderField("Location")); 	return urlConnection.getHeaderField("Location"); }

인데요....

String postDataForm = "{\n\"snippet\": {\n\"title\": \"%s\",\n\"description\": \"%s\",\n\"tags\": "  		+ "[\"cool\", \"video\", \"more keywords\"],\n\"categoryId\": 22\n},\n\"status\": "  		+ "{\n\"privacyStatus\": \"public\",\n\"embeddable\": True,\n\"license\": \"youtube\"\n}\n}"; String postData = String.format(postDataForm, title, description, tags); DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.writeBytes(postData); dos.flush(); dos.close();
이 부분이 없으면 일단 업로드는 되요. 다만 title, description, tags 이런 정보가 업로드 되지 않고 동영상만 업로드 되네요...
(동영상 업로드는 위의 메소드에서 받은 리턴값을 이용해서 다른 메소드에서 처리합니당)

암만 생각해봐도 postDataForm 부분이 잘못된 거 같은데요..... 어케 해야 하는걸까요 ㅜㅜ
어제 저녁부터 새벽까지 저거 하나땜에 고생이네요.... 요리 바꾸고 조리 바꾸고 해봤는데 계속 실패 ㅜㅜ
도움이 필요합니다~
꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호