2012-09-07 4 views
0

웹에서 HTML을 가져오고 싶지만 할 수 없습니다. 여기 내 코드는안드로이드 웹에서 Html 가져 오기

final EditText et = (EditText) findViewById(R.id.editText1); 
    final TextView tv = (TextView) findViewById(R.id.textView1); 
    final Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      try { 
       URL url = null; 
       url = new URL(et.getText().toString()); 
       URLConnection conn = url.openConnection(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        tv.append(line); 

       } 
      } catch (Exception e) { 
       Log.e("error", "erorr connection "+ e.toString()); 
      }    
     } 
    }); 

나는 또한 이미 인터넷에서 허가를 추가했다. 4.0.3

+0

나는 – kongkea

+0

http://stackoverflow.com/questions/6466719/manipulating-data-on-webs-in-android/6466749#6466749 – Rasel

+0

솔루션 여기을 http 텍스트 뷰에 표시 할 웹에서 HTML을 얻고 싶은 : // stackoverflow.com/questions/12327625/error-when-get-html-from-web-in-android/12328074#12328074 –

답변

1

에 넣어주세요 네트워크 부품을 만들기 대상 버전은 전경 UI 스레드에 업데이트합니다. AsyncTask를 사용하여 그렇게 할 수 있습니다.

3

//는 HTTP 클라이언트에게 백그라운드 스레드에

HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet("http:// yoururl"); 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) 
     sb.append(line + "\n"); 

    String resString = sb.toString(); // Result is here 

    is.close(); // Close the stream