java readline()方法读取所有行吗? [英] does java Readline() method read all Lines ?!

查看:585
本文介绍了java readline()方法读取所有行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。即时开发Android应用程序。这个应用程序必须返回Html源代码.acctualy它连接到一个网站,必须返回其Html源。

但它返回不同的东西!

从某些线路跳出来!它没有阅读所有这些,我不知道,也许它只是读取对线!

我在源代码中有30行,但它只显示其中的14行!

我会告诉你我的java代码,Html和最后的输出。

请看看它并帮助我。



这是我的java代码:



  public   class  MainActivity  extends 活动{

受保护 字符串 ShowHtml ()
{
// TextView myTxt =(TextView)findViewById(R.id.txtReturn );
int LineCounter = 0;
HttpURLConnection urlConnection = null;
字符串 line = ;
尝试
{
网址myUrl =新网址( < span class =code-string> http://10.0.2.2:80/mob1/Default.aspx);
// 网址myUrl =新网址(http://tabnak.ir);
urlConnection =(HttpURLConnection)myUrl.openConnection();
BufferedReader in = new BufferedReader( new InputStreamReader(urlConnection.getInputStream()));

while (in.readLine()!= null)
{
line = line + in.readLine( )的ToString();
LineCounter ++;
}


}
catch (exception ex0)
{
Log.d( 错误:,ex0.toString());

}
最后
{
如果(的URLConnection!= NULL){urlConnection.disconnect(); }


}
int Count = line.length();
return line = 计数:+String.valueOf(Count)+ 行计数器 + LineCounter + / Start XML: + line;

}

class RetrieveFeedTask extends AsyncTask< String,Void,String> {



protected String doInBackground(< span class =code-sdkkeyword> String
... urls){
try
{
return ShowHtml();
}
catch (例外e)
{
return e.toString();
}
}

受保护 void onPostExecute( String 结果){
TextView mytxt2 =(TextView)findViewById(R.id.txtReturn);
mytxt2.setText(Result);

}
}

@覆盖
protected void onCreate(Bundle savedInstanceState){
super .onCreate( savedInstanceState);
setContentView(R.layout.activity_main);
new RetrieveFeedTask()。execute();
}



}





Html源代码:< br $> b $ b

 <   html     xmlns   = < span class =code-keyword> http://www.w3.org/1999/xhtml >  
< head > < title >
Hello
< / title > < / head >
< body >
< 表单 名称 = form1 method < span class =code-keyword> = post 操作 = Default.aspx id = form1 >
< div >
< 输入 type = 隐藏 名称 = __ VIEWSTATE id = __ VIEWSTATE value = / wEPDwUJNzgzNDMwNTMzZGRE1XcQ3ivQexlso8SYFW5dafVfqQ == / >
< / div >


< div >
< div > position2 < / div >
< div > position3 < / div >
< div > position4 < / div >
< div > position5 < / div >
< div > position6 < / div >
< div > position7 < / div >
< div > position8 < / div >


< / div >


< / form >
< / body >
< / html >





申请表输出:



 <   head  >  <   title  >  
< / title > < span class =code-keyword>< / head >
< 表格 名称 = form1 方法 = 发布 action = Default.aspx id = form1 >
< < span class =code-leadattribute> div
>
< div > position3 < / div >
< div > position5 < / div >
< div > position7 < / div >
< / div >
< / body >

解决方案

  while (in.readLine()!= null){
林E =线+ in.readLine()的toString();
LineCounter ++;
}



你读了一行(在语句中),但你没有保存它任何地方。然后,您阅读下一行并将其添加到您的变量中。然后你回去读另一条你忽略的行等等。


hi guys . im developing an android application . this application must return Html Source code .acctualy it connects to a web site and must return its Html Source .
But It returns something difrent !
it jumps from some lines ! it does'nt read all of them , i dont know , maybe it just reads pair lines !
i have 30 lines in Source Code but it shows only 14 of them !
i will show you my java code, the Html and the output at the end .
pls look at it and help me .

Here This is my java Code :

public class MainActivity extends Activity {	
	
	protected String ShowHtml()
	{
		//TextView myTxt=(TextView)findViewById(R.id.txtReturn); 
		   int LineCounter=0;
		HttpURLConnection urlConnection = null;
		String line="";
		try
		{
	    URL myUrl=new URL("http://10.0.2.2:80/mob1/Default.aspx");
	//	URL myUrl=new URL("http://tabnak.ir");
		urlConnection = (HttpURLConnection)myUrl.openConnection();		
		BufferedReader in = new BufferedReader (new InputStreamReader(urlConnection.getInputStream()));		
     
		while(in.readLine()!=null)
		{			
			line=line+in.readLine().toString();		
			LineCounter++;
		}
	
	
		}
		catch(Exception ex0)
		{
		Log.d("Error : ",ex0.toString());

		}
		finally
		{
			if(urlConnection!=null){urlConnection.disconnect(); }
			
			
		}
		int Count=line.length();
		return line="Count: "+String.valueOf(Count)+"Line Counter"+LineCounter+"/ Start XML : "+line;
		
	}	
	
	class RetrieveFeedTask extends AsyncTask<String, Void, String> {

	    

	    protected String doInBackground(String... urls) {
	    	try
	    	{
	        return ShowHtml();
	    	}
	    	catch(Exception e)
	    	{
	    	return e.toString();
	    	}
	    }

	    protected void onPostExecute(String Result) {
	    	TextView mytxt2=(TextView)findViewById(R.id.txtReturn);
	    	mytxt2.setText(Result);
	      
	    }
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new RetrieveFeedTask().execute();
	}

	

}



Html Source Code :

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
	Hello
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGRE1XcQ3ivQexlso8SYFW5dafVfqQ==" />
</div>


<div>
<div>position2</div> 
<div>position3</div> 
<div>position4</div> 
<div>position5</div> 
<div>position6</div>
<div>position7</div> 
<div>position8</div> 
 
   
</div>


</form>
</body>
</html>



Application Output :

<head><title>
</title></head>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div> 
<div>position3</div>
<div>position5</div>
<div>position7</div>
</div>
</body>

解决方案

while(in.readLine()!=null) {			
    line=line+in.readLine().toString();		
    LineCounter++;
}


You read a line (in the while statement), but you do not save it anywhere. Then you read the next line and add it to your variable. Then you go back and read another line which you ignore, etc, etc.


这篇关于java readline()方法读取所有行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆