I need help in http connection???

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

I need help in http connection???

Postby sit06603 » Tue Sep 30, 2008 9:22 am

hi all,

I not quite familiar with android... hoping I can get some answer here... thanks...
I have two questions here...

1st Question
---------------
How can I use android to call a url and get the inputStream to get the content of the pages???


Code in J2me:-
-------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  connection = (HttpConnection) Connector.open(Info.DeviceInfo.URL + "?cmd=gses&userid=" + userSession);
  2.  
  3.  connection.setRequestMethod(HttpConnection.GET);
  4.  
  5.  
  6.  
  7. if (connection.getHeaderField("Set-Cookie") != null) {
  8.  
  9.          cookie = connection.getHeaderField("Set-Cookie");
  10.  
  11.   }
  12.  
  13.  
  14.  
  15. DataInputStream dis = connection.openDataInputStream();
  16.  
  17.  
  18.  
  19. int ch;
  20.  
  21. String str = "";
  22.  
  23.  
  24.  
  25. while ((ch = dis.read()) != -1) {
  26.  
  27.          str += (char) ch;
  28.  
  29. }
  30.  
  31.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



2nd Question
----------------
How can I get Stream Connection from a server using Android??


Coding in J2me
------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. connection=(StreamConnection)Connector.open(DeviceInfo.URL_CONNECTION);
  2.  
  3.  
  4.  
  5. inpStream = connection.openInputStream();
  6.  
  7. outStream = connection.openOutputStream();     
  8.  
  9.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



anyone have any example can show me???




wesley.
sit06603
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 15, 2008 11:22 am

Top

Postby ninor » Tue Sep 30, 2008 10:21 am

Have you tried this code that you posted?

Along with the code, you'll need some permissions into the AndroidManifest.xml
See this:
http://code.google.com/android/reference/android/Manifest.permission.html
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby sit06603 » Tue Sep 30, 2008 10:27 am

yup... I have go through...

HttpConnection the class seem to be very different with those in J2ME one... and StreamConnection is not available in android(mayb is others class has the similar functionality, but I dunno how)...


any ideas??? hoping can get some help here or probably some example...

thanks...

Wesley.
sit06603
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu May 15, 2008 11:22 am

Postby ninor » Tue Sep 30, 2008 11:03 am

And any of these Direct and Indirect subclasses of InputStream?
http://code.google.com/android/reference/java/io/InputStream.html

I'm sorry that I can't be of much more help in this topic..
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby charlie.collins » Tue Sep 30, 2008 12:34 pm

There are a lot of examples around that use HTTP, and there are many ways to do it in Android. You can use a straight up URL and HttpConnection from the java.net packages, or you can use the Apache HTTP stuff.

http://code.google.com/android/referenc ... mmary.html

http://code.google.com/android/referenc ... mmary.html

Just for a quick and dirty URL connection and stream you can do something like the following:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3. String result = null;
  4.  
  5. URL url = null;
  6.  
  7.                 try {
  8.  
  9.                         url = new URL("http://youre.hostname.tld.path/here");
  10.  
  11.                 } catch (MalformedURLException e) {
  12.  
  13.                         Log.e(e.getMessage());
  14.  
  15.                 }
  16.  
  17.  
  18.  
  19.                 if (url != null) {
  20.  
  21.                         try {
  22.  
  23.                                 HttpURLConnection urlConn = (HttpURLConnection) url
  24.  
  25.                                                 .openConnection();
  26.  
  27.                                 BufferedReader in = new BufferedReader(new InputStreamReader(
  28.  
  29.                                                 urlConn.getInputStream()));
  30.  
  31.                                 String inputLine;
  32.  
  33.  
  34.  
  35.                                 int lineCount = 0; // limit the lines for the example
  36.  
  37.                                 while ((lineCount < 10) && ((inputLine = in.readLine()) != null)) {
  38.  
  39.                                         lineCount++;
  40.  
  41.                                         result += "\n" + inputLine;
  42.  
  43.                                 }
  44.  
  45.                                
  46.  
  47.                                 in.close();
  48.  
  49.                                 urlConn.disconnect();
  50.  
  51.  
  52.  
  53.                         } catch (IOException e) {
  54.  
  55.                                 Log.e(e.getMessage());
  56.  
  57.                         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


You're direct J2ME code probably won't work in Android, but there are many very easy networking options available - as you can see.
charlie.collins
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Sep 30, 2008 12:24 pm

Postby ninor » Thu Oct 02, 2008 6:05 am

THanks for your help, charlie
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests