webview显示不出来

yayaya 发布于 2011/08/26 11:45
阅读 4K+
收藏 0
package net.worldrop.internalstoragedemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

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));
					

					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);   
			        
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
		            Log.d(this.toString(), "Error!");
				}

			}
		}
	};
}
我现在做的这个是要有一个rememeber me的功能,就是可以记住上次输入的内容,然后再用搜索的button来拿到上次输入的内容,并且从url拿到整个的html 但是现在按搜索键,只能够显示上次输入的内容,却不能够显示出来我要的html 求帮助
加载中
0
无鸯
无鸯

权限设置了吗?

AndroidManifest.xml中必须使用许可"android.permission.INTERNET"

否则会出Web page not available错误。

0
y
yayaya
有啊,我有设置,但是就是出不来……
0
曾赛
曾赛

如果能在 117 行到 123 行中的某个合适的位置,加上一句 

wv.loadData(myString, "text/html", "utf-8");
可能就好了 ,有点粗心哦 

返回顶部
顶部