반응형

구현

	private class NewHttpGetRequest extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... params){
			HttpsURLConnection urlConnection= null;
			java.net.URL url = null;
			String response = "";
			try {
				result=null;
				url  = new java.net.URL(params[0]);
				urlConnection=(HttpsURLConnection)url.openConnection();
				urlConnection.setRequestMethod("GET");
				urlConnection.setDoOutput(true);
				urlConnection.setDoInput(true);
				urlConnection.connect();

				int resCode = urlConnection.getResponseCode();
				Log.i(TAG,"## urlConnection.getResponseCode() :"+resCode);

				if (resCode == 200){
					InputStream inStream = null;
					inStream = urlConnection.getInputStream();
					BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
					String temp = "";
					while((temp = bReader.readLine()) != null){
						response += temp;
					}
					bReader.close();
					inStream.close();
					result = response;
				} else {
					result = "";
				}

				urlConnection.disconnect();

				Log.i(TAG,"## response :" +response);
			} catch (Exception e) {
				e.printStackTrace();
				result = "";
			}
			return result;
		}

		@Override
		protected void onPostExecute(String s) {
			super.onPostExecute(s);
			//파일 없을 경우 처리
			if(result.equals("")){
				//파일 없을경우
			} else {
				//파일 있을경우
			}
		}
	}

사용

try {
	new NewHttpGetRequest().execute("your url").get();
} catch (ExecutionException e) {
	e.printStackTrace();
} catch (InterruptedException e) {
	e.printStackTrace();
}
반응형

+ Recent posts