Desperately need help in building a Proxy Server for Android
===

I am building a Proxy Server for android and I have hit a wall. I need help desperately. I am quite new to Android. 
So, at first, I made the Proxy server in Java SE. The Proxy Server works as following:

1. It listens to a local port,

2. When the client connects, a new thread is created.

3. The HOST is extracted from the request. 

4. Client's request is then forwarded to the Remote address (acquired from HOST). 

5. The Proxy then reads the remote server's address until -1 is found.

6. This response is then forwarded to the client.
	
The proxy server is run in the Laptop. The client is Youtube APP of an android phone.
The phone is rooted and I have installed ProxyDroid to tell the client where the Proxy Server is.
"Global Proxy" option of ProxyDroid is used.
	
The code in Java SE:

        import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.net.InetAddress;
        import java.net.NetworkInterface;
        import java.net.ServerSocket;
        import java.net.Socket;
        import java.util.Enumeration;
        
        class clientThread implements Runnable
        {
        	Socket clientSocket = null;//each client socket.
        	
        	Socket connectYoutubeServerSock = null;//This socket is used to write client's request to the 
        									//YouTube Server.
        	int videoFlag = 0;//this flag determines whether the GET request is a video request or not. 
        	
        	final int BufferSize = 8019;
        	public String hostURL;
        	int youtubePort = 80;
        	//public DataOutputStream sendClientResponse = null;
            
            public clientThread(Socket clientSocket)
            {
            	this.clientSocket = clientSocket;
            }
        
        	
        	public void run()
        	{
        		System.out.println("Client connected: "+clientSocket.toString());
        		System.out.println("Current Thread Name: "+Thread.currentThread().getName());
        		
        		try
        		{
        			byte youtubeAppReqArray[] = new byte[BufferSize];//this byte array will house youtube app's request!
        			InputStream youtubeAppReq = clientSocket.getInputStream();
        	
        			// reading the request and put it into youtubeAppReqArray.
        			int bytesRead = youtubeAppReq.read(youtubeAppReqArray,0,BufferSize);
        			String youtubeAppReqString = new String(youtubeAppReqArray, 0, bytesRead);
        			System.out.println(youtubeAppReqString);
        	
        			// extract the url of the GET request!
        			int gStart = youtubeAppReqString.indexOf("GET: ") + 4;
        			int gEnd = youtubeAppReqString.indexOf('\n', gStart);
        			String gURL = youtubeAppReqString.substring(gStart, gEnd - 9);
        			System.out.println("URL of GET: " + gURL);
        			//test if videoplayback is there or not?
        			if(gURL.indexOf("videoplayback") > -1  )
        			{
        				System.out.println("This is a video request!");
        				videoFlag = 1;
        			}
        				
        			
        			// extract the host to connect to
        			int hStart = youtubeAppReqString.indexOf("Host: ") + 6;
        			int hEnd = youtubeAppReqString.indexOf('\n', hStart);
        			String host = youtubeAppReqString.substring(hStart, hEnd - 1);
        			System.out.println("Connecting to host " + host);
        			
        			//forward the youtube app request from proxy to the youtube server
        			Socket youtubeServerSocket = new Socket(host, 80);
        			OutputStream writeToYoutubeServerStream = youtubeServerSocket.getOutputStream();
        			System.out.println("Forwarding request to server");
        			writeToYoutubeServerStream.write(youtubeAppReqArray, 0, bytesRead);
        			writeToYoutubeServerStream.flush(); 
        			
        			// forward the response from the server to the browser
        			byte youtubeServerResArray[] = new byte[BufferSize];//this byte array will house youtube server's response.
        			
        			InputStream readFromYoutubeServerStream = youtubeServerSocket.getInputStream();
        			OutputStream writeToYoutubeAppStream = clientSocket.getOutputStream();
        
        			System.out.println("Forwarding request from server");
        
        			int remoteBytesRead;
        			do 
        			{
        				remoteBytesRead = readFromYoutubeServerStream.read(youtubeServerResArray,0,BufferSize);
        				System.out.println("Receiving " + remoteBytesRead + " bytes");
        				if (remoteBytesRead > 0) 
        				{
        					writeToYoutubeAppStream.write(youtubeServerResArray, 0, remoteBytesRead);
        					String rData = new String(youtubeServerResArray,0,remoteBytesRead);
        					System.out.println("Remote data:  "+rData);
        				}
        			} while (remoteBytesRead > 0); 
        			
        			writeToYoutubeAppStream.flush();
        			youtubeServerSocket.close();
        			clientSocket.close();
        			System.out.println("End of communication");
        		}
        		catch(IOException ioe)
        		{
        			System.out.println("Error"+ioe.getMessage());
        		}
        	}
        }
        
        public class ChallengeProxy extends IOException 
        {
        	public static final int SERVERPORT = 4447;
        	public static int threadName = 0;
        	public static void main(String[] args) 
        	{
        		ChallengeProxy proxyObj = new ChallengeProxy();
        		proxyObj.knowIP();//get the IP address of the local machine!
        		
        		 try 
        	     {
        			 ServerSocket serverSocket = new ServerSocket(SERVERPORT);
        	         System.out.println("Started on: "+SERVERPORT);
        	         
        	         while(true)//Non stop listen for clients!
        	         {
        		         Socket clientSocket = serverSocket.accept();//blocks until client is connected!
        	           	 System.out.println("Client connected: "+clientSocket.toString());
        	           	 
        	           	 Thread t = new Thread(new clientThread(clientSocket), Integer.toString(threadName));
        	           	 t.start();
        	           	 
        	           	 threadName++;
        	         }
        	     } 
        	     catch (IOException ioe) 
        	     {
        	    	System.out.println("Could not listen on port: "+SERVERPORT);
        	    	System.out.println("Error"+ioe.getMessage());
        	     }
        		 catch(Exception e)
        		 {
        			 System.out.println("Error"+e.getMessage());
        		 }
        	}
        
        	public void knowIP()
        	{
        		try 
        		{
        	         Enumeration e = NetworkInterface.getNetworkInterfaces();
        	 
        	         while(e.hasMoreElements()) 
        	         {
        	            NetworkInterface ni = (NetworkInterface) e.nextElement();
        	            System.out.println("Net interface: "+ni.getName());
        	 
        	            Enumeration e2 = ni.getInetAddresses();
        	 
        	            while (e2.hasMoreElements())
        	            {
        	               InetAddress ip = (InetAddress) e2.nextElement();
        	               System.out.println("IP address: "+ ip.toString());
        	            }
        	         }
        	     }
        	     catch (Exception e) 
        	     {
        	         e.printStackTrace();
        	     }
        	}
    
    }


The output looks something like:

    Started on: 4442
    Client connected: Socket[addr=/130.233.194.28,port=54978,localport=4442]
    Client connected: Socket[addr=/130.233.194.28,port=54978,localport=4442]
    Current Thread Name: 0
    POST http://www.google.com/m/appreq/mobilevideo HTTP/1.1
    Content-Type: application/binary
    Content-Length: 167
    Host: www.google.com
    User-Agent: Android-YouTube/2 (GT-S5570 GINGERBREAD); gzip
    Proxy-Connection: close
    Connection: close
    
    ㄀㄀㄀yyoutube,2.1.6,[GT-S5570][samsung/GT-S5570/GT-S5570:2.3.4/GINGERBREAD/XWKQG:user/release-keys],mvapp-android-samsung,en_USSpmXÓÞ㄀g㄀㄀㄀㄀㄀㄀㄀㄀g:log:ev㄀㄀㄀㄀è
    URL of GET: T http://www.google.com/m/appreq/mobilevideo 
    Connecting to host www.google.com
    Client connected: Socket[addr=/130.233.194.28,port=54979,localport=4442]
    Client connected: Socket[addr=/130.233.194.28,port=54979,localport=4442]
    Current Thread Name: 1
    GET http://gdata.youtube.com/feeds/api/users/asadfffx/newsubscriptionvideos?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none HTTP/1.1
    GData-Version: 2
    X-GData-Device: device-id="AOuj_RqmoX-WkCNNaJKieF2mmwMlkOMFRk7sQsKP_wmdrL1BB1N9V_iVIJAUBkvvyzGdpxWVS83wE7UkGPYjWf0BWvbPa0Uoo0cmgKfxzEqOog8EC-Rm1Wg", data="H1NEdDZ+FuL4U9v3Bx1hlVIAm1s="
    Authorization: BLA, BLA, BLA.
    Host: gdata.youtube.com
    User-Agent: Android-YouTube/2
    Proxy-Connection: close
    Connection: close
    
    
    URL of GET:  http://gdata.youtube.com/feeds/api/users/asadfffx/newsubscriptionvideos?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none 
    Connecting to host gdata.youtube.com
    Forwarding request to server
    Forwarding request from server
    Receiving 410 bytes
    Receiving -1 bytes
    End of communication
    Forwarding request to server
    Forwarding request from server
    Receiving 521 bytes
    Receiving 1641 bytes
    Receiving 5 bytes
    Receiving -1 bytes
    End of communication
    Client connected: Socket[addr=/130.233.194.28,port=54980,localport=4442]
    Client connected: Socket[addr=/130.233.194.28,port=54980,localport=4442]
    Current Thread Name: 2
    GET http://gdata.youtube.com/feeds/api/users/asadfffx/recommendations?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none HTTP/1.1
    GData-Version: 2
    X-GData-Device: device-id="AOuj_RqmoX-WkCNNaJKieF2mmwMlkOMFRk7sQsKP_wmdrL1BB1N9V_iVIJAUBkvvyzGdpxWVS83wE7UkGPYjWf0BWvbPa0Uoo0cmgKfxzEqOog8EC-Rm1Wg", data="Rf7tX44/Y4exqU5dhL4qS3Evy/I="
    Authorization: BLA, BLA, BLA."
    Host: gdata.youtube.com
    User-Agent: Android-YouTube/2
    Proxy-Connection: close
    Connection: close
    
    
    URL of GET:  http://gdata.youtube.com/feeds/api/users/asadfffx/recommendations?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none 
    Connecting to host gdata.youtube.com
    Forwarding request to server
    Forwarding request from server
    Receiving 522 bytes
    Receiving 4096 bytes
    Receiving 4096 bytes
    Receiving 4290 bytes
    Receiving 8019 bytes
    Receiving 8019 bytes
    ----------------MORE ----------------
    Receiving 8019 bytes
    Receiving 798 bytes
    Receiving -1 bytes
    End of communication
    Client connected: Socket[addr=/130.233.194.28,port=54981,localport=4442]
    Client connected: Socket[addr=/130.233.194.28,port=54981,localport=4442]
    Current Thread Name: 3
    GET http://i.ytimg.com/vi/Ud3mMj0AZZk/3.jpg HTTP/1.1
    Host: i.ytimg.com
    User-Agent: Android-YouTube/2
    Proxy-Connection: close
    Connection: close
    
    --------------SIMILAR REQUESTS ARE NOT SHOWN!----------------------
    
    URL of GET:  http://i.ytimg.com/vi/e18My9d4lAo/3.jpg 
    Receiving 8019 bytes
    Connecting to host i.ytimg.com
    Receiving 8019 bytes
    Forwarding request to server
    Forwarding request from server
    Receiving 8019 bytes
    --------------SAME OUTPUT------------------
    Receiving 1043 bytes
    Receiving -1 bytes
    End of communication
    Receiving 8019 bytes
    --------------SAME OUTPUT------------------
    Receiving 8019 bytes
    Receiving 8019 bytes
    Receiving 8019 bytes
    Client connected: Socket[addr=/130.233.194.28,port=55013,localport=4442]
    Receiving 8019 bytes
    Client connected: Socket[addr=/130.233.194.28,port=55013,localport=4442]
    Current Thread Name: 33
    GET http://i.ytimg.com/vi/AwYwdt8uLNA/3.jpg HTTP/1.1
    Host: i.ytimg.com
    User-Agent: Android-YouTube/2
    Proxy-Connection: close
    Connection: close
    
    
    URL of GET:  http://i.ytimg.com/vi/AwYwdt8uLNA/3.jpg 
    Connecting to host i.ytimg.com
    Forwarding request to server
    Forwarding request from server
    Receiving 3916 bytes
    Receiving 1178 bytes
    Receiving -1 bytes
    End of communication
    Receiving 8019 bytes
    Receiving 8019 bytes
    Receiving 8019 bytes
    
    .............. A lot more "Receiving 8019 bytes" here, which I have omitted.  
    
    Receiving 8019 bytes
    Receiving 8019 bytes
    Receiving 7002 bytes
    Receiving -1 bytes
    End of communication


This is exactly what I want. Now, I transform the code a little, so that, it runs in Android.
However, the logic underneath is the same. Here also, when a new client connects, a new thread is created. The Youtube APP is still the client. So now, the YouTube APP would request a proxy server residing in the same Android machine. As before, ProxyDroid is configured so that, YouTube APP knows where my Proxy server is running.  
The code for Android looks like:

    
    public class ChallengeAndroidProxyActivity extends Activity 
    {
    	public static final int SERVERPORT = 4450;
    	public static int threadName = 0;
    	/** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            knowIP();//find the IP address of the native machine.
            		 //If needed use this IP address in ProxyDroid.	
            
            try 
    	     {
    			 ServerSocket serverSocket = new ServerSocket(SERVERPORT);
    			 Log.i("Started on: ",""+SERVERPORT);
    	         
    	         while(true)//Non stop listen for clients!
    	         {
    		         //sleep
    	        	 //Thread.sleep(20);//sleep for 20 milliseconds.
    	        	 Socket clientSocket = serverSocket.accept();//blocks until client is connected!
    	           	 System.out.println("Client connected: "+clientSocket.toString());
    	           	 
    	           	 Thread t = new Thread(new clientThread(clientSocket), Integer.toString(threadName));
    	           	 t.start();
    	           	 
    	           	 threadName++;
    	         }
    	     } 
    	     catch (IOException ioe) 
    	     {
    	    	 Log.i("Could not listen on port: ",""+SERVERPORT);
    	    	 Log.i("Error",ioe.getMessage());
    	     }
    		 catch(Exception e)
    		 {
    			 Log.i("Error",e.getMessage());
    		 }
            
        }
    
        class clientThread implements Runnable
        {
        	Socket clientSocket = null;//each client socket.
        	
        	Socket connectYoutubeServerSock = null;//This socket is used to write client's request to the 
        									//YouTube Server.
        	int videoFlag = 0;//this flag determines whether the GET request is a video request or not. 
        	
        	final int BufferSize = 8019;
        	public String hostURL;
        	int youtubePort = 80;
        	//public DataOutputStream sendClientResponse = null;
            
            public clientThread(Socket clientSocket)
            {
            	this.clientSocket = clientSocket;
            }
    
        	
        	public void run()
        	{
        		Log.i("Client connected: ",clientSocket.toString());
        		Log.i("Current Thread Name: ",Thread.currentThread().getName());
        		
        		try
        		{//1
        			byte youtubeAppReqArray[] = new byte[BufferSize];//this byte array will house youtube app's request!
        			InputStream youtubeAppReq = clientSocket.getInputStream();
        	
        			// reading the request and put it into youtubeAppReqArray.
        			int bytesRead = youtubeAppReq.read(youtubeAppReqArray,0,BufferSize);
        			String youtubeAppReqString = new String(youtubeAppReqArray, 0, bytesRead);
        			Log.i("Youtube App Request: ",youtubeAppReqString);
        	
        			// extract the url of the GET request!
        			int gStart = youtubeAppReqString.indexOf("GET: ") + 4;
        			int gEnd = youtubeAppReqString.indexOf('\n', gStart);
        			String gURL = youtubeAppReqString.substring(gStart, gEnd - 9);
        			Log.i("URL of GET: ",gURL);
        			//test if videoplayback is there or not?
        			if(gURL.indexOf("videoplayback") > -1  )
        			{//2
        				Log.i("Yes!","This is a video request!");
        				videoFlag = 1;
        			}//2
        				
        			// extract the host to connect to
        			int hStart = youtubeAppReqString.indexOf("Host: ") + 6;
        			int hEnd = youtubeAppReqString.indexOf('\n', hStart);
        			String host = youtubeAppReqString.substring(hStart, hEnd - 1);
        			Log.i("Connecting to host ",host);
        			
        			//forward the youtube app request from proxy to the youtube server
        			Socket youtubeServerSocket = new Socket(host, 80);
        			OutputStream writeToYoutubeServerStream = youtubeServerSocket.getOutputStream();
        			Log.i("State: ","Forwarding request to server");
        			writeToYoutubeServerStream.write(youtubeAppReqArray, 0, bytesRead);
        			writeToYoutubeServerStream.flush(); 
        			
        			//re-initialize the youtube request byte array!
        			//byte byteFillValue = (byte)0xFF;
        			//Arrays.fill(youtubeAppReqArray, byteFillValue);
        			
        			// forward the response from the server to the browser
        			byte youtubeServerResArray[] = new byte[BufferSize];//this byte array will house youtube server's response.
        			
        			InputStream readFromYoutubeServerStream = youtubeServerSocket.getInputStream();
        			OutputStream writeToYoutubeAppStream = clientSocket.getOutputStream();
    
        			Log.i("State: ","Forwarding request from server");
    
        			int remoteBytesRead;
        			do 
        			{//2
        				//sleep
        				//Thread.sleep(20);//sleep for 20 milliseconds. 
        				remoteBytesRead = readFromYoutubeServerStream.read(youtubeServerResArray,0,BufferSize);
        				Log.i("State: ","Receiving " + remoteBytesRead + " bytes");
        				if (remoteBytesRead > 0) 
        				{//3
        					writeToYoutubeAppStream.write(youtubeServerResArray, 0, remoteBytesRead);
        					String rData = new String(youtubeServerResArray,0,remoteBytesRead);
        					Log.i("Remote data: ",rData);
        				}//3
        			} while (remoteBytesRead > 0);//2 
        			
        			writeToYoutubeAppStream.flush();
        			youtubeServerSocket.close();
        			clientSocket.close();
        			Log.i("State: ","End of communication");
        		}//1
        		//catch(InterruptedException e)
        		//{
        		//	Log.i("Error while sleep(): ",e.getMessage());	
        		//}
        		catch(IOException ioe)
        		{//1
        			Log.i("Error",ioe.getMessage());
        		}//1
        	}
        }
        
        
        
        /************************************************************************************/
        public void knowIP()
    	{
    		try 
    		{
    	         Enumeration e = NetworkInterface.getNetworkInterfaces();
    	 
    	         while(e.hasMoreElements()) 
    	         {
    	            NetworkInterface ni = (NetworkInterface) e.nextElement();
    	            Log.i("Net interface: ",ni.getName());
    	 
    	            Enumeration e2 = ni.getInetAddresses();
    	 
    	            while (e2.hasMoreElements())
    	            {
    	               InetAddress ip = (InetAddress) e2.nextElement();
    	               Log.i("IP address: ",ip.toString());
    	            }
    	         }
    	     }
    	     catch (Exception e) 
    	     {
    	         e.printStackTrace();
    	     }
    	}
    }

However, this does not work! Even the HTTP request that I get from the Youtube APP is somehow screwed up. The output looks like:

    

        07-18 21:38:44.889: I/URL of GET:(12402):  http://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp:
    -------------"//gdata.youtube.com" IS REPEATED!------------------------------------------------
    //gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.com/feeds/api/users/asadfffx/newsubscriptionvideos?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none 
            07-18 21:38:44.889: I/Connecting to host(12402): gdata.youtube.com
            07-18 21:38:44.889: I/State:(12402): Forwarding request to server
            07-18 21:38:44.889: I/System.out(12402): Client connected: Socket[addr=/192.168.1.129,port=33875,localport=4453]
            07-18 21:38:44.889: I/State:(12402): Forwarding request from server
            07-18 21:38:44.899: I/Client connected:(12402): Socket[addr=/192.168.1.129,port=33875,localport=4453]
            07-18 21:38:44.899: I/Current Thread Name:(12402): 1517
            07-18 21:38:44.899: I/Youtube App Request:(12402): GET http://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp:
    -------------"//gdata.youtube.com" IS REPEATED!------------------------------------------------
    //gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.com/feeds/api/users/asadfffx/newsubscriptionvideos?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none HTTP/1.1
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): GData-Version: 2
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): X-GData-Device: device-id="AOuj_RqmoX-WkCNNaJKieF2mmwMlkOMFRk7sQsKP_wmdrL1BB1N9V_iVIJAUBkvvyzGdpxWVS83wE7UkGPYjWf0BWvbPa0Uoo0cmgKfxzEqOog8EC-Rm1Wg", data="H1NEdDZ+FuL4U9v3Bx1hlVIAm1s="
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): Authorization: GoogleLogin auth="DQAAAKcAAAC5aU6IN0t6yUkoiU9OmjU8fYjKm1m7MLKleHLhtR-uwCclnZGcKGm-6gYfkjvAGKGXK88dbKnB708CVCQkvJofL6smBXp7TEFj1FqGaRasdOx2iVYrbaTbWtIc8sBaga7v2P1BfctMiSTtEU2HWCqqCiubMvpfspnl9wS284SLgk-Se4jjFsZTo-84la_2wJy_Wmap8OSTdif7JoaxpxYLAWECByrJO50iMTj__6cN3A"
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): Host: gdata.youtube.com
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): User-Agent: Android-YouTube/2
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): Proxy-Connection: close
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): Connection: close
            
            07-18 21:38:44.899: I/Youtube App Request:(12402): 
            
            
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            
        07-18 21:38:46.259: I/Connecting to host(12402): gdata.youtube.com
            07-18 21:38:46.259: I/State:(12402): Forwarding request to server
            07-18 21:38:46.269: I/State:(12402): Forwarding request from server
            07-18 21:38:46.269: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.269: I/State:(12402): Receiving -1 bytes
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
        
        07-18 21:38:46.369: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.369: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.379: I/State:(12402): End of communication
            07-18 21:38:46.379: I/State:(12402): End of communication
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            07-18 21:38:46.379: I/State:(12402): End of communication
            07-18 21:38:46.379: I/State:(12402): End of communication
            07-18 21:38:46.379: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.379: I/State:(12402): Receiving -1 bytes
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            07-18 21:38:46.479: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.479: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.479: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.489: I/State:(12402): End of communication
            07-18 21:38:46.489: I/State:(12402): End of communication
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            07-18 21:38:46.489: I/State:(12402): End of communication
            07-18 21:38:46.489: I/State:(12402): Receiving -1 bytes
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------    07-18 21:38:46.529: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.529: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.529: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.529: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.539: I/State:(12402): End of communication
            07-18 21:38:46.539: I/State:(12402): End of communication
            07-18 21:38:46.539: I/State:(12402): Receiving -1 bytes
        -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------    
            07-18 21:38:46.569: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.569: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.579: I/State:(12402): End of communication
            -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            07-18 21:38:46.589: I/State:(12402): End of communication
            07-18 21:38:46.589: I/State:(12402): End of communication
            07-18 21:38:46.599: I/State:(12402): Receiving -1 bytes
            -------------SIMILAR LOOKING REQUESTS ARE NOT SHOWN!---------------
            07-18 21:38:46.639: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.639: I/State:(12402): Receiving -1 bytes
            07-18 21:38:46.649: I/State:(12402): Receiving -1 bytes
            
            -------------- SIMILAR "End of communication" STATEMENTS. THEREFORE, NOT SHOWN.--------------- 
            
            07-18 21:38:46.809: I/State:(12402): End of communication
            07-18 21:38:46.809: I/State:(12402): End of communication
            07-18 21:38:46.809: I/State:(12402): End of communication
            07-18 21:38:46.879: I/State:(12402): End of communication
    
    You can see, in the program, I print out the Youtube APP's request. In the Java SE version, where
    the Youtube APP of android is connecting to my Proxy running in a Laptop, the HTTP request is correct. But in the Android machine, the HTTP request received by the proxy is totally screwed. 
    For example:
    
        07-18 21:38:44.479: I/Youtube App Request:(12402): GET http://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp:
    -------------"//gdata.youtube.com" IS REPEATED!------------------------------------------------
    //gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.comhttp://gdata.youtube.com/feeds/api/users/asadfffx/newsubscriptionvideos?format=2%2C3%2C9&start-index=1&max-results=10&safeSearch=none HTTP/1.1

Why does the GET request look like this? Also u might notice in the output:

    07-18 21:38:44.479: I/State:(12402): Forwarding request to server
    07-18 21:38:44.489: I/System.out(12402): Client connected: Socket[addr=/192.168.1.129,port=33847,localport=4453]
    07-18 21:38:44.489: I/State:(12402): Forwarding request from server

But I am not seeing the log statement that shows how many bytes were read from the remote server.
In other words, the program is not executing the following block where data is read from the remote machine and forwarded to the YouTube App. 

    int remoteBytesRead;
    do 
    {//2
    	//sleep
    	//Thread.sleep(20);//sleep for 20 milliseconds. 
    	remoteBytesRead = readFromYoutubeServerStream.read(youtubeServerResArray,0,BufferSize);
    	Log.i("State: ","Receiving " + remoteBytesRead + " bytes");
    	if (remoteBytesRead > 0) 
    	{//3
    		writeToYoutubeAppStream.write(youtubeServerResArray, 0, remoteBytesRead);
    		String rData = new String(youtubeServerResArray,0,remoteBytesRead);
    		Log.i("Remote data: ",rData);
    	}//3
    } while (remoteBytesRead > 0);//2 

Why is this happening? Am I constructing the program wrong? Is the problem related to my generating a new thread for each client?

Any help/ suggestion would be appreciated? Can anyone suggest some code on how to construct a proxy for android? 



