마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

webApp 관련 header 가져오기 질문

0 추천

webView를 이용해 개발중인 웹사이트를 띄우는거는 정상이나 

웹쪽에 전반적인지식이 부족한 상태에서 알아보면서 개발진행중입니다.

우선 헤더 부분을 찍어서 클라이언트 쪽에서 요청 시 관련 정보를 띄우는 식 으로 이해를 하고 진행하려고 많은 자료를 찾아서 진행해 보았으나 정상적인 작동이 안되는문제 때문에 질문드립니다.

우선적으로 제가 진행해본 방법은 

private void getHeader() {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					headerURL = new URL(url);
					URLConnection conn = headerURL.openConnection();
					Map<String, List<String>> headers = conn.getHeaderFields();
					for(Map.Entry<String, List<String>> entry : headers.entrySet()) {
						Log.e("Key : "+ entry.getKey() , " Value : " + entry.getValue());
					}
					
					
				} catch (MalformedURLException e) {
					Log.e("HeaderUrl Error", "HeaderUrl Parse Error");
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			       
			}
		});
	}

이런식으로 메소드 구현후 진행 해보았습니다. 스레드를 미사용시 http exception 으로 thread를 사용해 진행 했습니다.

허나 웹 url페이지는 정상적으로 나타났으나 log가 안직혀서 진행할수가 없었습니다. 또 찾아본 부분은 

 

webView.setWebViewClient(viewClient);
		viewClient.shouldOverrideUrlLoading(webView, url);

웹뷰에 webviewClient를 별도로 구현해서 진행 하는방법을 보고 진행해봣으나 이번꺼는 url페이지 조차 동작을 하지 안습니다.

public class HeaderWebViewClient extends WebViewClient {
	private URL url;
	private URLConnection conn;
	@Override
	public boolean shouldOverrideUrlLoading(final WebView view, final String urlAddr) {
		Log.e("HeaderWebView headerSetting","");
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
				    url = new URL(urlAddr);
				    conn = url.openConnection();
				    conn.setConnectTimeout(3000);
				    conn.setRequestProperty("valuekey", "ddddd");
				    conn.connect();
				    // get the size of the file which is in the header of the request
				    int size = conn.getContentLength();
				   }catch (Exception e) {
				    // TODO: handle exception
				   }
				   String htmlContent = "";
				   HttpGet httpGet = new HttpGet(urlAddr);
				   // this receives the response
				   HttpResponse response;
				   HttpClient httpclient = new DefaultHttpClient();
				   try {
				    response = httpclient.execute(httpGet);
				    if (response.getStatusLine().getStatusCode() == 200) {
				     // la conexion fue establecida, obtener el contenido
				     HttpEntity entity = response.getEntity();
				     if (entity != null) {
				      InputStream inputStream = entity.getContent();
				      StringBuffer out = new StringBuffer();
				      
				      byte[] b = new byte[4096];
				      for (int n; (n = inputStream.read(b)) != -1;) {
				          out.append(new String(b, 0, n));
				      }
				      
				      htmlContent = out.toString();
				     }
				    }
				   } catch (Exception e) {}
				   view.loadData(htmlContent, "text/html", "utf-8");
				
			}
		});
		return true;
	}
}

이런식의 구현방법 이였습니다. 

webview내부에서 요청에 대한 부분을 디바이스에서 헤더라는것을 찾아오는 방법에 대해 혹시 진행해보신분 계시다면 도움부탁드립니다.

구기웅 (1,110 포인트) 님이 2014년 2월 21일 질문
구기웅님이 2014년 2월 21일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...