看板 AndroidDev 關於我們 聯絡資訊
照著minksable大修改了一下CODE如下.. 可是不知道為何TRY裡面的IF ELSE都不會執行? TEXTVIEW都是只SHOW到... try's loop TextView getwebdata = (TextView) findViewById (R.id.getdata); getwebdata.setText("...start to try connect" ); HttpGet get = new HttpGet("http://www.google.com"); try { getwebdata.setText("... try's loop" ); //HttpClient hc = new DefaultHttpClient(); HttpResponse echo = new DefaultHttpClient().execute(get); if (echo.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String is = EntityUtils.toString(echo.getEntity()); getwebdata.setText("...connect OK" ); //getwebdata.setText(is ); } else {getwebdata.setText("...connect fail" ); } } catch (IOException e) { e.printStackTrace(); } ※ 引述《Magicx (270度的鳥顧之相)》之銘言: : 最近想寫個讓程式擷取網頁原始碼資料並SHOW在TEXTVIEW已確定動作有成功~ : 參考網路上許多相關CODE發現有用HTTPURLCONNECT的..HTTPPOST的.. : 後來覺得這個寫法似乎比較簡潔...可是不知道為何都沒有反應... : 請問是哪裡有問題呢?? 感謝指教~~ : TextView getwebdata = (TextView) findViewById (R.id.getdata); : String savegetwebdata =getwebdata.getText().toString(); : try { : HttpClient hc = new DefaultHttpClient(); : HttpGet get = new HttpGet("http://www.google.com"); : HttpResponse echo = hc.execute(get); : if (echo.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { : //getwebdata.setText(savegetwebdata+"HttpStatus SC_OK" ); : InputStream is = echo.getEntity().getContent(); : //savegetwebdata =getwebdata.getText().toString(); : getwebdata.setText(savegetwebdata+is ); : } : } catch (Exception e) { : // TODO: handle exception : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.34.226.28
minksable:請問有加permission嗎? "android.permission.INTERNET" 09/03 01:17
minksable:因為我用你的code也是成功的喔~ 09/03 01:17
Magicx:自問自答...看看才看到你的推文..真的是permission問題 09/03 01:35
Magicx:JAVA初心者真的是瞎子摸象...>< 09/03 01:36
minksable:加油~ 我也是android的初心者 XD 09/03 01:40