Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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 :

Java
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
<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 :

HTML
<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>
Posted
Updated 14-Dec-14 3:12am
v5

1 solution

Java
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.
 
Share this answer
 
Comments
Maciej Los 14-Dec-14 9:22am    
+5
Richard MacCutchan 14-Dec-14 9:31am    
Thanks, took me a few minutes to spot it though.
seyed mahmud shahrokni 14-Dec-14 9:34am    
tnx man . you are my personal hero !
Richard MacCutchan 14-Dec-14 10:00am    
Happy to help. We all learn something from these questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900