text off the internet


Last Updated:

  1. ireddit

    ireddit New Member This Topic's Starter

    Joined:
    Sep 2, 2010
    Messages:
    2
    Likes Received:
    0
    For the last 5 days I have searched high and low both on the net, and through various books at Barnes and Nobel and I cannot find a plain and easy to understand answer on how to grab simple text strings from a server. The ideal way would be to grab data out of a mysql database. I know that there is a way to do it locally on the phone out of a sqlite database, but I want it to come off the internet as my information will change daily.

    I'm more than just a novice programmer, but I am not the full blown programming geek I wish I could be... but I can find my way around PHP pretty well. But this Android stuff is completely foreign.

    I have eclipse installed and did the whole hello world thing. It was pretty straight forward, and I got the XML design down too. I understand the purpose of the manifest. Beyond that calling in these different classes and activities that I assume are required leave me stumped.

    Can someone please post a place where I can download an example WORKING project that explains how to grab simple strings of code off a server? Documentation is also very helpful.

    THANKS IN ADVANCE!
     

    Advertisement
  2. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    2 things:
    1. You could use a database connector to connect directly to the database.
    2. You could write a php page which outputs xml or text (whatever you want) and you can use the org.apache.http.* classes to download the generated content from your php page.

    I suggest number 2 because you already know your way around php.

    here's a code snippet of how to use the org.apache.http.* classes.

    Code (Text):
    1.  
    2. String url = "http://somehost.com/somepage.php?somevariable=something";
    3. HttpClient client = new DefaultHttpClient();
    4. HttpGet request = new HttpGet(url);
    5.  
    6. HttpResponse response = client.execute(request);
    7. StatusLine status = response.getStatusLine();
    8. if (status.getStatusCode() != HTTP_STATUS_OK) {
    9.     throw new Exception("Network error");
    10. }
    11.  
    12. HttpEntity entity = response.getEntity();
    13. InputStream is = entity.getContent();
    14.  
    15. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    16.  
    17. byte[] buffer = new byte[512];
    18. int bytesRead = 0;
    19. while ((bytesRead = is.read(buffer)) != -1) {
    20.     byteStream.write(buffer, 0, bytesRead);
    21. }
    22.  
    23. String source = new String(byteStream.toByteArray());
    24.  
     
  3. ireddit

    ireddit New Member This Topic's Starter

    Joined:
    Sep 2, 2010
    Messages:
    2
    Likes Received:
    0
    Okay I think I understand where you are coming from.... I'm sorry to sound ignorant, but where do you put that code? I tried pasting it into the .java file in the src folder and a bunch of errors popped up. Take it slow and please be as descriptive as possible. You help is greatly appreciated.
     
  4. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    You probably want tot put this code into a function with a string parameter (url) and a return value of type String.
    This way you can call the function whenever you have to download something.

    Challenge: Designing for Responsiveness | Android Developers
    You can create an AsyncTask which you can call to download something. This way your app doesn't block while downloading.

    Remember, google is your friend, and if you have further questions you may ask them here.

    ps. I might post an AsyncTask snippet here tommorow morning.
     

Share This Page

Loading...