2014-01-15 60 views
0

因此,我一直試圖將網站的代碼(本網站僅包含html/php)保存到android中的字符串中。將網站的HTML代碼保存爲字符串Android

現在,我已經嘗試了ASyncTask,並且無法讓它工作。我已經閱讀了整個紀錄片..我不得不承認,我是Android的首發。 儘管如此,出於某種原因,我所嘗試過的所有東西都使它崩潰或無法正常工作。

現在,我問你一個簡單的例子,如何獲得一個網站的代碼,並將其設置爲一個字符串。

在此先感謝!

編輯:

MainTabView.java

package com.example.projectnova; 


import java.net.URL; 

import com.example.projectnova.R.string; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TabHost; 
import android.widget.Toast; 
import android.widget.TabHost.TabSpec; 

public class MainTabView extends Activity implements OnClickListener{ 

    TabHost th; 
    TabSpec specs; 
    Button search; 
    EditText searchText; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_tab_view); 

     search = (Button)findViewById(R.id.searchButton); 
     searchText = (EditText)findViewById(R.id.searchInputText); 
     th = (TabHost)findViewById(R.id.tabhost); 
     search.setOnClickListener(this); 


     th.setup(); 

      specs = th.newTabSpec("tag1"); 
      specs.setContent(R.id.Search); 

      specs.setIndicator("Search"); 
      th.addTab(specs); 

      specs = th.newTabSpec("tag2"); 
      specs.setContent(R.id.Download); 
      specs.setIndicator("Downloads"); 
      th.addTab(specs); 


      /* 
      * 
      * Example in (I believe)PHP 
      * 
      function streaminfo($file,$port) { 
      global $src; 
      $fp = @fsockopen ($file, $port, &$errno, &$errstr, 5); 
      if (!$fp) { 
      echo "Could not connect to <b>{$file}:{$port}</b> ({$errno}) - {$errstr}\n"; 
      } else { 
      fputs ($fp, "GET /7 HTTP/1.1\r\nUser-Agent:Mozilla\r\n\r\n"); 
      while (!feof($fp)) { 
      $stream = fgets($fp,1024); 
      } 
      list(,$stream) = explode("<body>",$stream); 
      list($stream) = explode("</body>",$stream); 
      list($user, $status, $user_peak, $user_max,$filler ,$bitrate, $song) = explode(",",$stream); 
      if($status== 0) { 
      } else { 
      $arr = array('nameofsong' => $song); 
      echo json_encode($arr); 
      } 
      fclose($fp); 
      } 
      } 
      streaminfo("188.138.79.175",8030); 
      */ 
    } 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     switch(arg0.getId()){ 
     case R.id.searchButton: 
      int i = searchText.getText().toString().length(); 

      if(i == 0){ 
       Context context = getApplicationContext(); 
       CharSequence text =("The search box is empty"); 
       int duration = Toast.LENGTH_SHORT; 

       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 
      } 
      else{ 

       String s = searchText.getText().toString(); 
       String htmlText = s.replace(" ","_"); // Works 
       String link = "WebsiteUrl.com/" + htmlText + ".html"; //Works 
       // searchText.setText(link); Test Purposes 
      } 

     } 
    } 

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
     protected Long doInBackground(URL... urls) { 
      int count = urls.length; 
      long totalSize = 0; 
      for (int i = 0; i < count; i++) { 
       totalSize += Downloader.downloadFile(urls[i]); 
       publishProgress((int) ((i/(float) count) * 100)); 
       // Escape early if cancel() is called 
       if (isCancelled()) break; 
      } 
      return totalSize; 
     } 

     protected void onProgressUpdate(Integer... progress) { 
      setProgressPercent(progress[0]); 
     } 

     protected void onPostExecute(Long result) { 
      showDialog("Downloaded " + result + " bytes"); 
     } 
    } 
} 
+1

我們不給的問題提供完整的工作的例子在這裏,但如果告訴你,你也嘗試什麼樣的代碼,我們可以指出你在哪裏錯了,所以我們可以修復它 – CodingIntrigue

+0

@RGraham Alrigh,再給我一次編輯。 – Paramone

+0

@Paramone在你的代碼中,你永遠不會調用DownloadFilesTask,我認爲這是問題 – GhostDerfel

回答

0

我可以問你爲什麼要這麼做?

HttpClient client = new DefaultHttpClient(); 
HttpGet request = new HttpGet(url); 
HttpResponse response = client.execute(request); 

String html = ""; 
InputStream in = response.getEntity().getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
StringBuilder str = new StringBuilder(); 
String line = null; 
while((line = reader.readLine()) != null) 
{ 
    str.append(line); 
} 
in.close(); 
html = str.toString(); 
+0

這將無法正常工作,因爲它會嘗試從主線程訪問網絡 – CodingIntrigue

+0

Oo不是Android上的模式下載的背景與AsyncTask什麼的? – GhostDerfel

+0

來自OP:「現在,我嘗試了AsyncTask,並且無法使其工作」。所以如果他不能工作,這個答案將無濟於事 – CodingIntrigue

相關問題