0

Gurus, I am a lil lost on this topic. Here's a breakdown of what I'm trying to do.

[User/Android Device] sends location info to a server -> [server]

[server] returns a list of items that has been sorted using sql from a - >[db]

[server] obtains table information and returns to user -> [User/Android Device]

so from the above situtation, I am lost as to how do i communicate with the server using eclipse? do i use php or asp in java(if it's possible :S) or could i be pointed to somewhere to a link to read on?

from the server there will be a php/asp page that handles the request correct?

Hope fellow gurus get what i'm trying to say

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
eddy chng
  • 3
  • 1
  • 2
  • I figured out the user > server part where i'd send to the server my values in the form of the hyperlink to a php server where the values will then be taken to be used for the sql statements. But, if i'm reading it right, there is no way that i can have the server to give me back a list of items straight to my device correct? but i have read somewhere that i can have xml output from json. So, im wondering if anyone knows where i can find more details and examples of such coding. a recap; device >long/lat > server server queries the db server >json/php/ >device am i missing something? – eddy chng Feb 12 '11 at 12:25

3 Answers3

2

call the following method to connect to any url and read response data from it. What so ever the URL you will hit. Url will be of any php or asp or jsp page or in any server side scripting language. The following method has nothing do with it. what so ever the url page will return that will be entered in your Log records. check Log cat for read result

 private void connectToServerAndReadData()
{
     HttpURLConnection conn;
     boolean result = false;

         try{
             // Enter any URL here you want to connect
             URL url = new URL("http://php1.funnymedialinks.com/scribd/rcheck.php");

            // Open a HTTP connection to the URL

             conn = (HttpURLConnection) url.openConnection();
            // conn.connect();
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              String line ;



              while ((line = rd.readLine()) != null) {

                       Log.v("Readed Data from Server ","data- "+line);

              }



            rd.close();


         }catch(MalformedURLException e){

                 e.printStackTrace();
         }
         catch(IOException e){
                 e.printStackTrace();               
         }
        catch(Exception e){
                 e.printStackTrace();           
         }





}
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
  • eddy chng if this answer helps you out then accept the answer by checking a arrow on the left side of answer so that others can also get a right reference. – Rohit Sharma Feb 11 '11 at 16:11
  • javanator; is it right to say that the only to get back a list of items from the server and displays it the way i want it ot be is through, a webview? or can i actually send a list of items that has been queried from a db back to my device? if yes how should i be doing it? hope you don't mind that i copy pasted from above ;) – eddy chng Feb 13 '11 at 14:21
  • its very easy infact.. See what we do when we design an application for android we seek some specific data from the web service.By hitting the url. but not only hitting the url resolve anything. For e.g consider me going on a shop and i say to the shop keeper that " GIVE ME SOMETHING ". how logical is that . and compare this to hitting the url. Even if he gave me the entire shop i need to filter what i need. Now consider me going on a shop and saying to the shopkeeper that "GIVE ME A PEN". This is logical and compare this with hitting the url and passing some parameter along. – Rohit Sharma Feb 14 '11 at 04:02
  • Now what so ever web service page that is to used along the app to provide data need to be designed and developed in correspondence with the android app. The API URL without parameter will like this http://www.myapi.com/index.php? and with parameter it will be like http://www.myapi.com/index.php?productName=pen&productID=123 now its the responsibity of index.php page code to retrieve the parameter passed and generate the content dynamically to read from the android app. Now generally its preferable for api to generate content in XML format and your android app need to use SAXParser. – Rohit Sharma Feb 14 '11 at 04:13
  • Now each time your android app depending upon some runtime information will create the url string with the dyanamic content parameter and its hitting will result into specific data retrieval. Thats it. hope it help :) and dont forget to accept the answer if it helps you. – Rohit Sharma Feb 14 '11 at 04:16
  • Thanx a lot javanator for answering :) but i think this might be my last question, hopefully! So from the asp site, which has queried the db, which then returns with a table of results. The results from the db will then be changed to xml like this example http://www.beansoftware.com/ASP.NET-Tutorials/Using-XML.aspx which means the result would be a .xml file correct? which then is deciphered using SAXParser that will break down the content of the page which i can dynamically 'decorate' my app right? – eddy chng Feb 14 '11 at 05:31
  • http://support.microsoft.com/default.aspx?scid=KB;EN-US;q301244&ID=KB;EN-US;q301244 is also another way which i found. so this 2 ways shows that i can return xml from an asp site but even though it returns a xml type, the link from my app would be ending with .asp if i choose to use asp correct? are there any examples that you know of that is relevant to my case? :) – eddy chng Feb 14 '11 at 05:39
  • yes you are very much right.. you consider the url you hit as the xml file. your url will be ending wth .aspx that you will give to pasrser object. everything will done automatically then. Read How to use SAX parser futher – Rohit Sharma Feb 14 '11 at 06:07
1

Sounds like you need an HTTP listener running on a server that takes a request that includes the location (latitude/longitude?) of an Android device, queries a database for a list of items, and returns that back to the Android device.

You'll also have to have a database with the schema created and data loaded.

If you have all that, the HTTP listener can be as simple as a Java servlet that accepts an HTTP GET request with two parameters, one each for latitude and longitude. Or you can make it as complex as a SOAP web service with a WSDL, request and response XML.

You would package the servlet as a Java web app in a WAR and deploy it locally on your development machine using a servlet/JSP engine like Tomcat.

Your Android device would have to know how to make an HTTP connection to the server, formulate the appropriate request, and consume the response when it comes back.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • thank you for the swift response duffymo :) The android end is pretty simple just the back end is slowing us down – eddy chng Feb 11 '11 at 13:58
  • duffymo; is it right to say that the only to get back a list of items from the server and displays it the way i want it ot be is through, a webview? or can i actually send a list of items that has been queried from a db back to my device? if yes how should i be doing it? am i getting my facts wrong here? :( – eddy chng Feb 13 '11 at 14:19
  • I don't know Android well enough to answer. The "facts" you're presenting are unclear to me. You should not send a ResultSet from the database to any UI; map the result into an object or collection and close the ResultSet in the scope of the method that created it - on the server side. – duffymo Feb 13 '11 at 16:08
0

You have few options actually.

i> You can access the SOAP based webservice methods implemented on the server side & call using the KSOAP API ( 3 rd party KSOAP jar needs to be added to the eclipse)

ii> You can also access RESTFul webservices implemented on the server side , which will either return you a XML or JSON as a response.

For mobile applications , better to go for RESTFul webservices which returns JSON.

Android is Java based but the Server based coding can be .NET , J2ee , Php . Actually its irrelevant to you since you need to access the webservice methods , if thinking from a mobile developer perspective.

You can setup J2ee , .NET , PHP environment with eclipse.

Kindly check the following url which can be useful to you for setting up .NET , J2ee , PHP on eclipse

http://sourceforge.net/projects/eclipsedotnet/

http://www.eclipse.org/pdt/

http://www.eclipse.org/downloads/moreinfo/jee.php

Kindly revert for any clarification.

chiranjib
  • 5,288
  • 8
  • 53
  • 82