显示网页内容的问题

时间:2011-09-19 04:16:19

标签: android search webview

我在我的项目中添加了这个记住我的功能,我需要在webview中显示搜索结果,但现在每次点击搜索按钮,都可以记住搜索内容,但是网页内容不能显示。


public class InternalStorageDemo extends Activity {

EditText txt;
Button writeBtn;
Button readBtn;
TextView tv;
WebView wv;

String FILE_NAME = "mFile"; 
String FILE_CONTENT; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    txt = (EditText)findViewById(R.id.txt);
    writeBtn = (Button)findViewById(R.id.writeBtn);
    readBtn = (Button)findViewById(R.id.readBtn);       
    writeBtn.setOnClickListener(listener);
    readBtn.setOnClickListener(listener);
}

View.OnClickListener listener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.writeBtn:
            FILE_CONTENT = txt.getText().toString().equals("")?"null":txt.getText().toString();
            try {
                FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
                fos.write(FILE_CONTENT.getBytes());
                fos.close();
                //Toast.makeText(InternalStorageDemo.this, "stored done", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.readBtn:
            FileInputStream fis;
            try {
                fis = openFileInput(FILE_NAME);
                byte[] input = new byte[fis.available()];
                while(fis.read(input) != -1){}
                txt.setText(new String(input));
                try{
                String str = txt.getText().toString(); 

                URL url = new URL("http://epoly.tp.edu.sg/tpapp/isistt/TTServlet?txtModule=StudentSearch&txtAction=GetTT&txtSelStudentID=" + str);
                URLConnection conn = url.openConnection();
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));

                // Read the contents line by line (assume it is text),
                // storing it all into one string
                String content ="";
                String line = reader.readLine();
                while (line != null) {
                    //adding on to the string (+=)
                    //"\n" goes to a new line so that it has a break
                    content += line + "\n";
                    line = reader.readLine();
                }

                //close reader after reading contents
                reader.close();
                //using substring to get html contents from a specific tag
                String myString = content.substring(content.indexOf("</script>"));


                int start = myString.indexOf("</script>");
                //if start less than 0, no contents from start tag found, 
                //nothing will be displayed in webview, error message will be logged
                if (start < 0) {
                    Log.d(this.toString(), "Academic calendar start tag not found");
                }
                else {
                    int end = myString.indexOf("</body>", start);
                    if (end < 0) {
                        Log.d(this.toString(), "Academic calendar end tag not found");
                    } else {
                        //load only <newcollection> tag
                        myString = "<html><body>" + myString.substring(start, end) + "</body></html>";
                    }

                }

                //display contents that have been extracted to webview
                WebView wv = (WebView)findViewById(R.id.wv);
                wv.getSettings().setBuiltInZoomControls(true); 
                //set the webview contents' size
                wv.setInitialScale(80);
                //wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);   
                //wv.loadData(myString, "text/html", "utf-8");
                wv.loadDataWithBaseURL("http://epoly.tp.edu.sg/tpapp/isistt/TTServlet?txtModule=StudentSearch&txtAction=GetTT&txtSelStudentID="+str, myString, "text/html", "UTF-8", "about:blank");

            }catch (IOException e) {
                e.printStackTrace();
                Log.d(this.toString(), "Error!");

            }} catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                //Log.d(this.toString(), "Error!");
            }break;
        }
    }
};

}

0 个答案:

没有答案