Trying to get OutputStream for http connection

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Trying to get OutputStream for http connection

Postby ohechkay » Wed Dec 02, 2009 6:56 am

I'm trying to http connect to a server and write a request parameters with a DataOutputStream but I haven't used any API besides org.apache.http libraries which use entities instead of direct streams. I've tried these entities but it hasn't been working. It connects fine but I receive nothing back from the server, presumably because the server didn't read the parameters correctly.

My client code looks like this but doesn't work.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 HttpClient httpClient = new DefaultHttpClient();
  3.  
  4.                
  5.  
  6.                 HttpPost request = new HttpPost(uri);
  7.  
  8.                
  9.  
  10.                 ByteArrayOutputStream os = new ByteArrayOutputStream();
  11.  
  12.                
  13.  
  14.                 DataOutputStream daos = new DataOutputStream(os);
  15.  
  16.                
  17.  
  18.                 daos.writeUTF(U1);
  19.  
  20.                 daos.writeUTF(P1);
  21.  
  22.                 daos.writeUTF("param");
  23.  
  24.                 daos.writeUTF("en");
  25.  
  26.                 daos.writeByte(6);
  27.  
  28.                 daos.writeInt(1);
  29.  
  30.                 daos.flush();
  31.  
  32.                 daos.close();
  33.  
  34.                
  35.  
  36.                 ByteArrayEntity bae = new ByteArrayEntity(os.toByteArray());
  37.  
  38.                 request.setEntity(bae);
  39.  
  40.                
  41.  
  42.                 HttpResponse response = httpClient.execute(request);
  43.  
  44.                 int status = response.getStatusLine().getStatusCode();
  45.  
  46.  
Parsed in 0.243 seconds, using GeSHi 1.0.8.4


What libraries does Android support that would establish the http connection and let me directly write to the output stream?
ohechkay
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 02, 2009 6:47 am

Top

Postby padde » Wed Dec 02, 2009 7:17 am

padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby ohechkay » Wed Dec 02, 2009 7:29 am

Thanks for the reply.

I just tried this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 String uri = URL;
  3.  
  4.                 URL url = new URL(uri + URLEncoder.encode(URL_PARAM));
  5.  
  6.                 Log.d("Connection", "Starting");
  7.  
  8.                 HttpURLConnection con = (HttpURLConnection)url.openConnection();
  9.  
  10.  
  11.  
  12.             con.setDoInput( true );
  13.  
  14.             con.setDoOutput( true );
  15.  
  16.             con.connect();
  17.  
  18.  
  19.  
  20.                 OutputStream os = con.getOutputStream();
  21.  
  22.                 DataOutputStream daos = new DataOutputStream(os);
  23.  
  24.  
  25.  
  26.                 daos.writeUTF(U1);
  27.  
  28.                 daos.writeUTF(P1);
  29.  
  30.                 daos.writeUTF("param");
  31.  
  32.                 daos.writeUTF("en");
  33.  
  34.                 daos.writeByte(6);
  35.  
  36.                 daos.writeInt(1);
  37.  
  38.                 daos.flush();
  39.  
  40.                 daos.close();
  41.  
  42.  
Parsed in 0.070 seconds, using GeSHi 1.0.8.4

But this times out at the three line setting con fields and connecting. If the lines are commented out, I get a ProtocolException: does not support output.

Help?
ohechkay
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 02, 2009 6:47 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests