
출처 : https://bablabs.com/openapi
안녕하세요 이제 막 개발을 시작한 초보자입니다.
위의 rest api를 이용하려고 하는데요, 설명처럼 토큰을 발급받고 헤더에 추가한 것 같은데 실행이 잘 안되어서요!ㅠ
try {
Log.d(TAG, REQUEST_URL);
URL url = new URL(REQUEST_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("accesstoken", "제 토큰");
int responseStatusCode = httpURLConnection.getResponseCode();
InputStream inputStream;
if (responseStatusCode == HttpURLConnection.HTTP_OK) {
inputStream = httpURLConnection.getInputStream();
} else {
inputStream = httpURLConnection.getErrorStream();
}
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
httpURLConnection.disconnect();
result = sb.toString().trim();
} catch (Exception e) {
result = e.toString();
}
http://webnautes.tistory.com/462 이 글을 바탕으로 코드를 실행해 봤는데, 방식이 잘못된 건가요?
아예 header라는 리소스를 post로 생성시켜야 하는건지,
인터페이스를 정의해서 원래 존재하는 헤더를 변경시켜야 하는건지조차 잘 모르겠어요ㅠ ㅠ
도움 주시면 감사하겠습니다!