Get echo from php

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Get echo from php

Postby Untouchab1e » Fri Jun 18, 2010 12:44 am

So, I have a php script up on the web that returns a simple echo string.

I just want to grab this string, pretty simple. I know it can be done through http get, but I just dont know how.

Any help would be much appreciated!

I tried this, but it only gives me: "BasicHttpResponse@44fd..."

Code: Select all
HttpClient httpClient = new DefaultHttpClient();

    try {
      String url = "http://connect-utb.com/ushout/handle_download.php";
      Log.d("phobos", "performing get " + url);
      HttpGet method = new HttpGet(new URI(url));
      HttpResponse response = httpClient.execute(method);
      String results;
      results = response.toString();
      shout_text.setText(results);
    }
    catch (Exception ex)
    {
    //Exception handling
    }
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Top

Re: Get echo from php

Postby fbarnard » Fri Jun 18, 2010 8:40 am

hi

a little bit different


Code: Select all
URL u = new URL(url);
               HttpURLConnection c = (HttpURLConnection) u
                     .openConnection();
               c.setRequestMethod("GET");
               c.setDoOutput(true);
               c.connect();

               InputStream in = c.getInputStream();


and the data you want is in the inputstream

Code: Select all
   HttpClient httpClient = new DefaultHttpClient();
                HttpGet method = new HttpGet("url");
                  HttpResponse response = httpClient.execute(method);
                 
               InputStream in = response.getEntity().getContent();


same here
fbarnard
Developer
Developer
 
Posts: 44
Joined: Tue Jun 15, 2010 12:31 pm

Re: Get echo from php

Postby Untouchab1e » Fri Jun 18, 2010 10:39 am

Thanks for your reply. Worked nicely!

However, I now have a second challenge..

The echo gives me a text string I now want to split up into two variables in my app.
So lets say the echo gives me: 42124a_string.
then I would want to put 42124 in one variable and a_string in another.

The numeric value always consist of 5 numbers.

Thanks
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Re: Get echo from php

Postby fbarnard » Fri Jun 18, 2010 12:06 pm

Hi

Regular expressions is your FRIEND....

http://regexlib.com


Expression "([0-9]{5})(.*)"

Code: Select all
Pattern pattern = Pattern.compile("([0-9]{5})(.*)");
Matcher m = pattern.matcher(str);


m should have two groups

http://developer.android.com/reference/java/util/regex/Matcher.html
fbarnard
Developer
Developer
 
Posts: 44
Joined: Tue Jun 15, 2010 12:31 pm

Re: Get echo from php

Postby Untouchab1e » Sat Jun 19, 2010 11:41 am

Thanks. I ended up using substrings though. Worked fine as well ;)
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests