2015-04-02 84 views
0

我想製作一個TextView或Imageview從互聯網上請求信息,就好像它是一個每天更新的newspapper,我想做類似的事情,但我不知道如何。從互聯網更新textview

我想我應該有一個服務器或東西,但有人可以解釋我的過程?以及哪個服務器或做什麼?

回答

0
first you need to enable the permission in the android manifest 
<uses-permission android:name="android.permission.INTERNET" /> 
than you can use a textView like this 
new Thread() { 
      @Override 
      public void run() { 
       String path ="http://host.com/info.txt"; // your webpage with text 
       URL u = null; 
       try { 
        u = new URL(path); 
        HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
        c.setRequestMethod("GET"); 
        c.connect(); 
        InputStream in = c.getInputStream(); 
        final ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
        byte[] buffer = new byte[1024]; 
        in.read(buffer); // Read from Buffer. 
        bo.write(buffer); // Write Into Buffer. 

        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          TextView text = (TextView) findViewById(R.id.TextView1); 
          text.setText(bo.toString()); 
          try { 
           bo.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 

       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (ProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
}.start(); 
+0

我可以在哪裏有一個免費的網頁「section」「text」?將本網站上的所有內容都返回給我的應用程序嗎?頭文件等等也將被返回.. – 2015-04-06 17:05:15

+0

編輯評論,我已經創建了一個在線免費服務器上的帳戶,但現在在cPanel上我不知道如何創建與上面編寫的代碼進行通信 – 2015-04-06 17:41:04

+0

你什麼意思 ? – dodo 2015-04-06 18:31:27