• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps text off the internet

ireddit

Lurker
Sep 2, 2010
2
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!
 
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:
String url = "http://somehost.com/somepage.php?somevariable=something";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);

HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() != HTTP_STATUS_OK) {
    throw new Exception("Network error");
}

HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

byte[] buffer = new byte[512];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
    byteStream.write(buffer, 0, bytesRead);
}

String source = new String(byteStream.toByteArray());
 
Upvote 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.
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones